|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.common.collect.ImmutableCollection<E>
com.google.common.collect.ImmutableMultiset<E>
public class ImmutableMultiset<E>
An immutable hash-based multiset. Does not permit null elements.
Its iterator orders elements according to the first appearance of the element among the items passed to the factory method. When the multiset contains multiple instances of an element, those instances are consecutive in the iteration order.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset |
---|
Multiset.Entry<E> |
Method Summary | ||
---|---|---|
boolean |
add(E element,
int occurrences)
Guaranteed to throw an exception and leave the collection unmodified. |
|
boolean |
contains(Object element)
Determines whether this multiset contains the specified element. |
|
static
|
copyOf(Iterable<? extends E> elements)
Returns an immutable multiset containing the given elements. |
|
static
|
copyOf(Iterator<? extends E> elements)
Returns an immutable multiset containing the given elements. |
|
int |
count(Object element)
Returns the number of occurrences of an element in this multiset (the count of the element). |
|
Set<E> |
elementSet()
Returns the set of distinct elements contained in this multiset. |
|
Set<Multiset.Entry<E>> |
entrySet()
Returns a view of the contents of this multiset, grouped into Multiset.Entry instances, each providing an element of the multiset and
the count of that element. |
|
boolean |
equals(Object other)
Compares the specified object with this multiset for equality. |
|
int |
hashCode()
Returns the hash code for this multiset. |
|
Iterator<E> |
iterator()
|
|
static
|
of()
Returns the empty immutable multiset. |
|
static
|
of(E... elements)
Returns an immutable multiset containing the given elements. |
|
int |
remove(Object element,
int occurrences)
Guaranteed to throw an exception and leave the collection unmodified. |
|
int |
removeAllOccurrences(Object element)
Guaranteed to throw an exception and leave the collection unmodified. |
|
int |
size()
|
|
String |
toString()
|
Methods inherited from class com.google.common.collect.ImmutableCollection |
---|
add, addAll, clear, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.google.common.collect.Multiset |
---|
add, containsAll, remove, removeAll, retainAll |
Methods inherited from interface java.util.Collection |
---|
addAll, clear, isEmpty, toArray, toArray |
Method Detail |
---|
public static <E> ImmutableMultiset<E> of()
public static <E> ImmutableMultiset<E> of(E... elements)
The multiset is ordered by the first occurrence of each element. For
example, ImmutableMultiset.of(2, 3, 1, 3)
yields a multiset with
elements in the order 2, 3, 3, 1
.
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements)
The multiset is ordered by the first occurrence of each element. For
example, ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3))
yields
a multiset with elements in the order 2, 3, 3, 1
.
Note that if c
is a Collection<String>
, then ImmutableMultiset.copyOf(c)
returns an ImmutableMultiset<String>
containing each of the strings in c
, while
ImmutableMultiset.of(c)
returns an
ImmutableMultiset<Collection<String>>
containing one element (the
given collection itself).
Note: Despite what the method name suggests, if elements
is an ImmutableMultiset
, no copy will actually be performed, and
the given multiset itself will be returned.
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements)
The multiset is ordered by the first occurrence of each element. For
example,
ImmutableMultiset.copyOf(Arrays.asList(2, 3, 1, 3).iterator())
yields a multiset with elements in the order 2, 3, 3, 1
.
NullPointerException
- if any of elements
is nullpublic int count(@Nullable Object element)
Multiset
Object.equals(java.lang.Object)
-based
multiset, this gives the same result as Collections.frequency(java.util.Collection>, java.lang.Object)
(which would presumably perform more poorly).
Note: the utility method Iterables.frequency(java.lang.Iterable>, java.lang.Object)
generalizes
this operation; it correctly delegates to this method when dealing with a
multiset, but it can also accept any other iterable type.
count
in interface Multiset<E>
element
- the element to count occurrences of
public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
public int size()
size
in interface Collection<E>
public boolean contains(@Nullable Object element)
Multiset
This method refines Collection.contains(java.lang.Object)
to further specify that
it may not throw an exception in response to element
being
null or of the wrong type.
contains
in interface Multiset<E>
contains
in interface Collection<E>
contains
in class ImmutableCollection<E>
element
- the element to check for
true
if this multiset contains at least one occurrence of
this elementpublic boolean add(E element, int occurrences)
add
in interface Multiset<E>
element
- the element to add occurrences of; may be null
only
if explicitly allowed by the implementationoccurrences
- the number of occurrences of this element to add. May
be zero, in which case no change will be made.
UnsupportedOperationException
- alwayspublic int remove(Object element, int occurrences)
remove
in interface Multiset<E>
element
- the element to conditionally remove occurrences ofoccurrences
- the number of occurrences of this element to remove. May
be zero, in which case no change will be made.
true
if the condition for modification was met. Unless
occurrences
is zero, this implies that the multiset was indeed
modified.
UnsupportedOperationException
- alwayspublic int removeAllOccurrences(Object element)
removeAllOccurrences
in interface Multiset<E>
element
- the element whose occurrences should all be removed
UnsupportedOperationException
- alwayspublic boolean equals(@Nullable Object other)
Multiset
true
if the given object is also a multiset and contains equal
elements with equal counts, regardless of order.
TODO: caveats about equivalence-relation.
equals
in interface Multiset<E>
equals
in interface Collection<E>
equals
in class Object
public int hashCode()
Multiset
(element == null ? 0 : element.hashCode()) ^ count(element)over all distinct elements in the multiset. It follows that a multiset and its entry set always have the same hash code.
hashCode
in interface Multiset<E>
hashCode
in interface Collection<E>
hashCode
in class Object
public String toString()
Multiset
It is recommended, though not mandatory, that this method return the
result of invoking Multiset.toString()
on the Multiset.entrySet()
, yielding a
result such as
[a x 3, c, d x 2, b x 0, e]
toString
in interface Multiset<E>
toString
in class ImmutableCollection<E>
public Set<E> elementSet()
Multiset
If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.
A common use for the element set is to find the number of distinct
elements in the multiset: elementSet().size()
.
elementSet
in interface Multiset<E>
public Set<Multiset.Entry<E>> entrySet()
Multiset
Multiset.Entry
instances, each providing an element of the multiset and
the count of that element. This set contains exactly one entry for each
distinct element in the multiset (thus it always has the same size as the
Multiset.elementSet()
). The order of the elements in the element set is
unspecified.
The entry set is backed by the same data as the multiset, so any change
to either is immediately reflected in the other. However, multiset changes
may or may not be reflected in any Entry
instances already
retrieved from the entry set (this is implementation-dependent).
Furthermore, implementations are not required to support modifications to
the entry set at all, and the Entry
instances themselves don't
even have methods for modification. See the specific implementation class
for more details on how its entry set handles modifications.
entrySet
in interface Multiset<E>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |