Guava Multiset接口

Multiset接口擴展設置有重複的元素,並提供了各種實用的方法來處理這樣的元素在集合中出現。

接口聲明

以下是com.google.common.collect.Multiset接口的聲明:

@GwtCompatible
public interface Multiset
extends Collection

接口方法

S.N.

方法及說明

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)
返回出現的元素的在該multiset的數目(元素的數量)。

6

Set elementSet()
返回集包含在此多集不同的元素。

7

Set<Multiset.Entry> entrySet()
返回此多集的內容的視圖,分組在Multiset.Entry實例中,每一個都提供了多集的一個元素和元素的計數。

8

boolean equals(Object object)
比較指定對象與此multiset是否相等。

9

int hashCode()
返回此multiset的哈希碼。

10

Iterator iterator()
返回一個迭代在這個集合中的元素。

11

boolean remove(Object element)
移除此多集multiset的單個出現的指定元素,如果存在。

12

int remove(Object element, int occurrences)
刪除了一些出現,從該多集multiset的指定元素。

13

boolean removeAll(Collection<?> c)
刪除所有這一切都包含在指定集合(可選操作)在此集合的元素。

14

boolean retainAll(Collection<?> c)
保持那些包含在指定collection(可選操作)在此只集合中的元素。

15

int setCount(E element, int count)
添加或刪除,使得該元素達到所期望的計數的元件的必要出現。

16

boolean setCount(E element, int oldCount, int newCount)
有條件設置元素的計數爲一個新值,如在setCount(對象,INT)中所述,條件是該元素預期的當前計數。

17

String toString()
返回該對象的字符串表示。

方法繼承

此接口繼承從以下接口方法:

  • java.util.Collection

Multiset 示例

使用所選擇的編輯器創建下面的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 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 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 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 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