Guava Multiset 接口
Multiset 接口扩展了 ‘Set’,允许存在重复元素,并提供了各种实用的方法来处理集合中这些元素的出现次数。
接口声明
下面是 com.google.common.collect.Multiset <E> 接口的声明 −
@GwtCompatiblepublic interface Multiset<E> extends Collection<E>
接口方法
| 序号 | 方法及描述 |
|---|---|
| 1 | boolean add(E element) 将指定元素添加到这个多重集合中一个实例。 |
| 2 | int add(E element, int occurrences) 将指定元素的多个实例添加到这个多重集合中。 |
| 3 | boolean contains(Object element) 判断这个多重集合是否包含指定元素。 |
| 4 | boolean containsAll(Collection <?> elements) 如果这个多重集合中至少包含指定集合中每个元素的一个实例,则返回true。 |
| 5 | int count(Object element) 返回此Multiset中元素的出现次数(元素的计数)。 |
| 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() 返回该对象的字符串表示形式。 |
继承的方法
这个接口继承了以下接口的方法—
java.util.CollectionMultiset的示例
使用您选择的任何编辑器创建以下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' : 5Total Size : 10Set [dbca]MultiSet [dbbbbbccaa]MultiSet [Element: d, Occurence(s): 1Element: b, Occurence(s): 5Element: c, Occurence(s): 2Element: a, Occurence(s): 2]Occurence of 'b' : 3
