Guava:Multiset 允许添加重复元素的SET
java guava    2018-09-01 00:02:35    0    0    0
cqc   java guava
Multiset 是
  •  一个允许添加重复元素的SET
  • Multiset中元素的顺序是无关的,即 {a, a, b} 和{a, b, a}是相等的
  • 它像是一个无顺序约束的ArrayList
  • 它像是一个Map<String, Integer>数据结构

以下提供一些常用的API用法

MethodDescription
count(E)Count the number of occurrences of an element that have been added to this multiset.
elementSet()View the distinct elements of a Multiset<E> as a Set<E>.
entrySet()Similar to Map.entrySet(), returns a Set<Multiset.Entry<E>>, containing entries supporting getElement() and getCount().
add(E, int)Adds the specified number of occurrences of the specified element.
remove(E, int)Removes the specified number of occurrences of the specified element.
setCount(E, int)Sets the occurrence count of the specified element to the specified nonnegative value.
size()Returns the total number of occurrences of all elements in the Multiset.


Multiset Is Not A Map

Note that Multiset<E> is not a Map<E, Integer>, though that might be part of a Multisetimplementation. Multiset is a true Collection type, and satisfies all of the associated contractual obligations. Other notable differences include:
  • A Multiset<E> has elements with positive counts only. No element can have negative counts, and values with count 0 are considered to not be in the multiset. They do not appear in the elementSet() or entrySet() view.
  • multiset.size() returns the size of the collection, which is equal to the sum of the counts of all elements. For the number of distinct elements, use elementSet().size(). (So, for example, add(E) increases multiset.size() by one.)
  • multiset.iterator() iterates over each occurrence of each element, so the length of the iteration is equal to multiset.size().
  • Multiset<E> supports adding elements, removing elements, or setting the count of elements directly. setCount(elem, 0) is equivalent to removing all occurrences of the element.
  • multiset.count(elem) for an element not in the multiset always returns 0.

Implementations

Guava provides many implementations of Multiset, which roughly correspond to JDK map implementations.
MapCorresponding MultisetSupports null elements
HashMapHashMultisetYes
TreeMapTreeMultisetYes (if the comparator does)
LinkedHashMapLinkedHashMultisetYes
ConcurrentHashMapConcurrentHashMultisetNo
ImmutableMapImmutableMultisetNo

SortedMultiset

SortedMultiset is a new variation on the Multiset interface that supports efficiently taking sub-multisets on specified ranges. For example, you could use latencies.subMultiset(0, BoundType.CLOSED, 100, BoundType.OPEN).size() to determine how many hits to your site had under 100ms latency, and then compare that to latencies.size() to determine the overall proportion.
TreeMultiset implements the SortedMultiset interface. At the time of writing, ImmutableSortedMultiset is still being tested for GWT compatibility.









上一篇: Guava Primitives(原型类相关的工具类)

下一篇: Guava: I/O utilities

文档导航