目录

Multiset

Multiset接口扩展'Set'以具有重复元素,并提供各种实用方法来处理集合中此类元素的出现。

接口声明 (Interface Declaration)

以下是com.google.common.collect.Multiset《E》界面的声明 -

@GwtCompatible
public interface Multiset<E>
   extends Collection<E>

接口方法

Sr.No 方法和描述
1

boolean add(E element)

向此multiset添加指定元素的单个匹配项。

2

int add(E element, int occurrences)

向此multiset添加多个元素。

3

boolean contains(Object element)

确定此多集合是否包含指定的元素。

4

boolean containsAll(Collection《?》 elements)

如果此多集包含指定集合中每个元素的至少一个匹配项,则返回true。

5

int count(Object element)

返回此多集中元素的出现次数(元素的计数)。

6

Set《E》 elementSet()

返回此multiset中包含的不同元素集。

7

Set《Multiset.Entry《E》》 entrySet()

返回此multiset内容的视图,该视图分组为Multiset.Entry实例,每个实例提供multiset的元素和该元素的计数。

8

boolean equals(Object object)

将指定对象与此multiset进行比较以获得相等性。

9

int hashCode()

返回此multiset的哈希码。

10

Iterator《E》 iterator()

返回此集合中元素的迭代器。

11

boolean remove(Object element)

从此多集中删除指定元素的单个匹配项(如果存在)。

12

int remove(Object element, int occurrences)

从此多集中删除多个指定元素。

13

boolean removeAll(Collection《?》 c)

删除此集合的所有元素,这些元素也包含在指定的集合中(可选操作)。

14

boolean retainAll(Collection《?》 c)

仅保留此集合中包含在指定集合中的元素(可选操作)。

15

int setCount(E element, int count)

添加或删除元素的必要匹配项,以使元素达到所需的计数。

16

boolean setCount(E element, int oldCount, int newCount)

有条件地将元素的计数设置为新值,如setCount(Object,int)中所述,前提是元素具有预期的当前计数。

17

String toString()

返回对象的字符串表示形式。

方法继承 (Methods Inherited)

此接口从以下接口继承方法 -

  • java.util.Collection

Multiset的示例

使用您选择的任何编辑器在C:/》 Guava.创建以下java程序C:/》 Guava.

GuavaTester.java

import java.util.Iterator;
import java.util.Set;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
public class GuavaTester {
   public static void main(String args[]) {
      //create a multiset collection
      Multiset<String> multiset = HashMultiset.create();
      multiset.add("a");
      multiset.add("b");
      multiset.add("c");
      multiset.add("d");
      multiset.add("a");
      multiset.add("b");
      multiset.add("c");
      multiset.add("b");
      multiset.add("b");
      multiset.add("b");
      //print the occurrence of an element
      System.out.println("Occurrence of 'b' : "+multiset.count("b"));
      //print the total size of the multiset
      System.out.println("Total Size : "+multiset.size());
      //get the distinct elements of the multiset as set
      Set<String> set = multiset.elementSet();
      //display the elements of the set
      System.out.println("Set [");
      for (String s : set) {
         System.out.println(s);
      }
      System.out.println("]");
      //display all the elements of the multiset using iterator
      Iterator<String> iterator  = multiset.iterator();
      System.out.println("MultiSet [");
      while(iterator.hasNext()) {
         System.out.println(iterator.next());
      }
      System.out.println("]");
      //display the distinct elements of the multiset with their occurrence count
      System.out.println("MultiSet [");
      for (Multiset.Entry<String> entry : multiset.entrySet()) {
         System.out.println("Element: " + entry.getElement() + ", Occurrence(s): " + entry.getCount());
      }
      System.out.println("]");
      //remove extra occurrences
      multiset.remove("b",2);
      //print the occurrence of an element
      System.out.println("Occurence of 'b' : " + multiset.count("b"));
   }
}

验证结果

使用javac编译器编译类如下 -

C:\Guava>javac GuavaTester.java

现在运行GuavaTester来查看结果。

C:\Guava>java GuavaTester

看到结果。

Occurence of 'b' : 5
Total Size : 10
Set [
d
b
c
a
]
MultiSet [
d
b
b
b
b
b
c
c
a
a
]
MultiSet [
Element: d, Occurence(s): 1
Element: b, Occurence(s): 5
Element: c, Occurence(s): 2
Element: a, Occurence(s): 2
]
Occurence of 'b' : 3
↑回到顶部↑
WIKI教程 @2018