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.