|
|||||||||
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
|
forSet(Set<E> set)
Returns a multiset view of the specified set. |
|
static
|
frequencyOrder(Multiset<?> multiset)
Returns a comparator that orders elements according to their increasing frequency in a multiset. |
|
static
|
immutableEntry(E e,
int n)
Returns an immutable multiset entry with the specified element and count. |
|
static
|
newEnumMultiset(Class<E> type)
Creates an empty EnumMultiset . |
|
static
|
newEnumMultiset(E... elements)
Creates an EnumMultiset containing the specified elements. |
|
static
|
newEnumMultiset(Iterable<E> elements)
Creates an EnumMultiset containing the specified elements. |
|
static
|
newHashMultiset()
Creates an empty HashMultiset using the default initial capacity
(16 distinct elements). |
|
static
|
newHashMultiset(E... elements)
Creates a HashMultiset containing the specified elements, using the
default initial capacity (16 distinct elements). |
|
static
|
newHashMultiset(Iterable<? extends E> elements)
Creates a 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).
public static <E> HashMultiset<E> newHashMultiset(E... elements)
HashMultiset
containing the specified elements, using the
default initial capacity (16 distinct elements).
elements
- the elements that the multiset should containpublic static <E> HashMultiset<E> newHashMultiset(Iterable<? extends E> elements)
HashMultiset
containing the specified elements.
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)
UnsupportedOperationException
.
multiset
- the multiset for which an unmodifiable view is to be
generated
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 use a ConcurrentMultiset
.
multiset
- the multiset to be wrapped
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 entry
IllegalArgumentException
- if n
is negativepublic 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. The multiset is threadsafe if the set is threadsafe.
set
- the backing set for the returned multiset viewpublic static <T> Ordering<T> frequencyOrder(Multiset<?> multiset)
m
is a non-empty
multiset of strings. Then:
Collections.max(m.elementSet(), frequencyOrder(m));returns a string that occurs most frequently in
m
.
(Warning: in this example, Collections.max
throws
NoSuchElementException
when m
is empty.)
The returned Ordering
is a view into the backing multiset, so
the ordering's behavior will change if the backing multiset changes. This
can be dangerous; for example, if the ordering 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 frequencies of the objects to
compare
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |