Guava - Multiset 接口
Multiset 接口扩展了"Set"以包含重复元素,并提供各种实用方法来处理集合中此类元素的出现。
接口声明
以下是b1接口的声明 −
@GwtCompatible public interface Multiset<E> extends Collection<E>
接口方法
| Sr.No | 方法及描述 |
|---|---|
| 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) 返回元素在此多重集中出现的次数(元素的计数)。 |
| 6 |
Set<E> elementSet() 返回此多重集中包含的不同元素的集合。 |
| 7 |
Set<Multiset.Entry<E>> entrySet() 返回此多重集内容的视图,分组为 Multiset.Entry 实例,每个实例提供多重集的一个元素以及该元素的计数。 |
| 8 |
boolean equals(Object object) 比较指定对象与此多重集是否相等。 |
| 9 |
int hashCode() 返回此多重集的哈希代码。 |
| 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.Collection
Multiset 示例
使用您在 C:/> Guava 中选择的任何编辑器创建以下 java 程序。
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
guava_collections_utilities.html

