|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.google.common.collect.Multisets
public final class Multisets
Provides static utility methods for creating and working with Multiset instances.
| Method Summary | ||
|---|---|---|
static
|
emptyMultiset()
Returns the empty multiset (immutable). |
|
static
|
forSet(Set<E> set)
Returns a multiset view of the specified set. |
|
static
|
frequencyOrder(Multiset<?> multiset)
Returns a comparator that imposes ascending frequency ordering on a collection of objects, using multiset to determine the frequency of
each object. |
|
static
|
immutableEntry(E e,
int n)
Returns an immutable multiset entry with the specified element and count. |
|
static
|
immutableMultiset()
Returns an immutable empty Multiset. |
|
static
|
immutableMultiset(E... elements)
Returns an immutable Multiset containing the specified elements. |
|
static
|
newEnumMultiset(Class<E> type)
Creates a new empty EnumMultiset. |
|
static
|
newEnumMultiset(E... elements)
Creates a new EnumMultiset containing the specified elements. |
|
static
|
newEnumMultiset(Iterable<E> elements)
Creates a new EnumMultiset containing the specified elements. |
|
static
|
newHashMultiset()
Creates a new empty HashMultiset using the default initial capacity
(16 distinct elements) and load factor (0.75). |
|
static
|
newHashMultiset(E... elements)
Creates a new HashMultiset containing the specified elements, using
the default initial capacity (16 distinct elements) and load factor
(0.75). |
|
static
|
newHashMultiset(Iterable<? extends E> elements)
Creates a new HashMultiset containing the specified elements. |
|
static
|
newTreeMultiset()
Creates an empty TreeMultiset instance. |
|
static
|
newTreeMultiset(Comparator<? super E> c)
Creates an empty TreeMultiset instance, sorted according to the
specified comparator. |
|
static
|
synchronizedMultiset(Multiset<E> multiset)
Returns a synchronized (thread-safe) multiset backed by the specified multiset. |
|
static
|
unmodifiableMultiset(Multiset<E> multiset)
Returns an unmodifiable view of the specified multiset. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <E> HashMultiset<E> newHashMultiset()
HashMultiset using the default initial capacity
(16 distinct elements) and load factor (0.75).
public static <E> HashMultiset<E> newHashMultiset(E... elements)
HashMultiset containing the specified elements, using
the default initial capacity (16 distinct elements) and load factor
(0.75).
elements - the elements that the multiset should containpublic static <E> HashMultiset<E> newHashMultiset(Iterable<? extends E> elements)
HashMultiset containing the specified elements. If
the specified elements is a Multiset, this method behaves
identically to HashMultiset.HashMultiset(Multiset). Otherwise, the
HashMultiset is created with the default initial capacity (16
distinct elements) and load factor (0.75).
elements - the elements that the multiset should containpublic static <E extends Comparable> TreeMultiset<E> newTreeMultiset()
TreeMultiset instance.
public static <E> TreeMultiset<E> newTreeMultiset(Comparator<? super E> c)
TreeMultiset instance, sorted according to the
specified comparator.
public static <E extends Enum<E>> EnumMultiset<E> newEnumMultiset(Class<E> type)
EnumMultiset.
public static <E extends Enum<E>> EnumMultiset<E> newEnumMultiset(Iterable<E> elements)
EnumMultiset containing the specified elements.
IllegalArgumentException - if elements is emptypublic static <E extends Enum<E>> EnumMultiset<E> newEnumMultiset(E... elements)
EnumMultiset containing the specified elements.
IllegalArgumentException - if elements is emptypublic static <E> Multiset<E> unmodifiableMultiset(Multiset<E> multiset)
multiset - the multiset for which an unmodifiable view is to be
returned
public static <E> Multiset<E> synchronizedMultiset(Multiset<E> multiset)
It is imperative that the user manually synchronize on the returned multiset when iterating over any of its collection views:
Multiset<E> m = Multisets.synchronizedMultiset(
new HashMultiset<E>());
...
Set<E> s = m.elementSet(); // Needn't be in synchronized block
...
synchronized (m) { // Synchronizing on m, not s!
Iterator<E> i = s.iterator(); // Must be in synchronized block
while (i.hasNext()) {
foo(i.next());
}
}
Failure to follow this advice may result in non-deterministic behavior.
For a greater degree of concurrency, you may wish to use a ConcurrentMultiset.
multiset - the multiset to be wrapped
public static <E> Multiset<E> emptyMultiset()
public static <E> Multiset.Entry<E> immutableEntry(E e,
int n)
e - the element to be associated with the returned entryn - the count to be associated with the returned entrypublic static <E> Multiset<E> forSet(Set<E> set)
remove operation) the results of
the iteration are undefined.
The multiset supports element removal, which removes the corresponding
element from the set. It does not support the add or addAll
operations.
The returned multiset will be serializable if the specified set is serializable.
set - the backing set for the returned multiset viewpublic static <E> Multiset<E> immutableMultiset()
Multiset. Equivalent to emptyMultiset(), except that the returned multiset's
remove methods throw an UnsupportedOperationException.
public static <E> Multiset<E> immutableMultiset(E... elements)
Multiset containing the specified elements.
Unlike an unmodifiable multiset such as that returned by unmodifiableMultiset(com.google.common.collect.Multiset, which provides a read-only view of an
underlying multiset which may itself be mutable, an immutable
multiset makes a copy of the original mappings, so that the returned
multiset is guaranteed never to change. This is critical, for
example, if the multiset is an element of a HashSet or a key in a
HashMap.
elements - the elements that the returned multiset should containpublic static <T> Comparator<T> frequencyOrder(Multiset<?> multiset)
multiset to determine the frequency of
each object. This enables a simple idiom for sorting (or maintaining)
collections (or arrays) of objects that are sorted by ascending frequency.
For example, suppose multiset is a multiset of strings. Then:
Collections.max(m.elementSet(), frequencyOrder(m));returns a string that occurs most frequently in
multiset.
The returned comparator is a view into the backing multiset, so the
comparator's behavior will change if the backing multiset changes. This can
be dangerous; for example, if the comparator is used by a TreeSet
and the backing multiset changes, the behavior of the TreeSet
becomes undefined. Use a copy of the multiset to isolate against such
changes when necessary.
multiset - the multiset specifying the frequency of objects to compare
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||