Guava - 多重集介面



Multiset 介面擴充套件了 'Set' 以包含重複元素,並提供各種實用程式方法來處理集合中此類元素的出現次數。

介面宣告

以下是 **com.google.common.collect.Multiset<E>** 介面的宣告:

@GwtCompatible
public 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)

返回此多重集中元素的出現次數(元素的計數)。

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 示例

使用您選擇的任何編輯器建立以下 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
guava_collections_utilities.htm
廣告

© . All rights reserved.