|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractCollection<E>
com.google.common.collect.AbstractMultiset<E>
com.google.common.collect.HashMultiset<E>
public final class HashMultiset<E>
Multiset implementation backed by a HashMap.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from interface com.google.common.collect.Multiset |
|---|
Multiset.Entry<E> |
| Constructor Summary | |
|---|---|
HashMultiset()
Constructs a new empty HashMultiset using the default initial
capacity. |
|
| Method Summary | ||
|---|---|---|
boolean |
add(E element,
int occurrences)
Adds a number of occurrences of an element to this multiset. Note that if occurrences == 1, this method has the identical effect to Multiset.add(Object). This method is functionally equivalent (except in the case
of overflow) to the call addAll(Collections.nCopies(element,
occurrences)), which would presumably perform much more poorly. |
|
protected java.util.Map<E,java.util.concurrent.atomic.AtomicInteger> |
backingMap()
|
|
int |
count(java.lang.Object element)
Returns the number of occurrences of an element in this multiset (the count of the element). |
|
static
|
create()
Creates a new empty HashMultiset using the default initial
capacity. |
|
static
|
create(int distinctElements)
Creates a new empty HashMultiset with the specified expected number
of distinct elements. |
|
static
|
create(java.lang.Iterable<? extends E> elements)
Creates a new HashMultiset containing the specified elements. |
|
protected java.util.Set<E> |
createElementSet()
Creates a new instance of this multiset's element set, which will be returned by AbstractMultiset.elementSet. |
|
java.util.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. |
|
java.util.Iterator<E> |
iterator()
|
|
protected void |
readObjectNoData()
Don't allow default serialization. |
|
int |
remove(java.lang.Object element,
int occurrences)
Conditionally removes a number of occurrences of an element from this multiset, provided that at least this many occurrences are present. |
|
int |
removeAllOccurrences(java.lang.Object element)
Removes all occurrences of the specified element from this multiset. |
|
protected void |
setBackingMap(java.util.Map<E,java.util.concurrent.atomic.AtomicInteger> backingMap)
Used during deserialization only. |
|
int |
size()
|
|
| Methods inherited from class com.google.common.collect.AbstractMultiset |
|---|
add, addAll, clear, contains, containsAll, elementSet, equals, hashCode, isEmpty, remove, removeAll, retainAll, toString |
| Methods inherited from class java.util.AbstractCollection |
|---|
toArray, toArray |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Collection |
|---|
toArray, toArray |
| Constructor Detail |
|---|
public HashMultiset()
HashMultiset using the default initial
capacity.
| Method Detail |
|---|
public static <E> HashMultiset<E> create()
HashMultiset using the default initial
capacity.
public static <E> HashMultiset<E> create(int distinctElements)
HashMultiset with the specified expected number
of distinct elements.
distinctElements - the expected number of distinct elements
java.lang.IllegalArgumentException - if distinctElements is negativepublic static <E> HashMultiset<E> create(java.lang.Iterable<? extends E> elements)
HashMultiset containing the specified elements.
elements - the elements that the multiset should containprotected java.util.Map<E,java.util.concurrent.atomic.AtomicInteger> backingMap()
protected void setBackingMap(java.util.Map<E,java.util.concurrent.atomic.AtomicInteger> backingMap)
public java.util.Set<Multiset.Entry<E>> entrySet()
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.
Invoking Multiset.Entry.getCount() on an entry in the returned
set always returns the current count of that element in the multiset, as
opposed to the count at the time the entry was retrieved.
entrySet in interface Multiset<E>entrySet in class AbstractMultiset<E>public int size()
AbstractMultisetThis implementation iterates across Multiset.entrySet() and
sums the counts of the entries.
size in interface java.util.Collection<E>size in class AbstractMultiset<E>public java.util.Iterator<E> iterator()
AbstractMultisetThis implementation usually invokes methods of the
Multiset.entrySet() iterator. However, the iterator's
remove method sometimes calls the multiset's remove.
iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in class AbstractMultiset<E>
public int count(@Nullable
java.lang.Object element)
AbstractMultisetObject.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.
This implementation iterates across Multiset.entrySet() and
sums the count of all entries.
count in interface Multiset<E>count in class AbstractMultiset<E>element - the element to count occurrences of
public boolean add(@Nullable
E element,
int occurrences)
occurrences == 1, this method has the identical effect to Multiset.add(Object). This method is functionally equivalent (except in the case
of overflow) to the call addAll(Collections.nCopies(element,
occurrences)), which would presumably perform much more poorly.
This implementation always throws an
UnsupportedOperationException. To support adding elements, override
it.
add in interface Multiset<E>add in class AbstractMultiset<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.
java.lang.IllegalArgumentException - if the call would result in more than
Integer.MAX_VALUE occurrences of element in this
multiset.
public int remove(@Nullable
java.lang.Object element,
int occurrences)
AbstractMultisetoccurrences, no change is made.
Note that if
occurrences == 1, this is functionally equivalent to the call
remove(element).
This implementation always throws an
UnsupportedOperationException. To support removing elements,
override it.
remove in interface Multiset<E>remove in class AbstractMultiset<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.
public int removeAllOccurrences(@Nullable
java.lang.Object element)
AbstractMultisetMultiset.remove(Object), which removes only
one occurrence at a time.
TODO: Nuke this. Use setCount(e, 0).
This implementation calls AbstractMultiset.remove(Object, int) with
Integer.MAX_VALUE occurrences.
removeAllOccurrences in interface Multiset<E>removeAllOccurrences in class AbstractMultiset<E>element - the element whose occurrences should all be removed
protected java.util.Set<E> createElementSet()
AbstractMultisetAbstractMultiset.elementSet.
createElementSet in class AbstractMultiset<E>
protected void readObjectNoData()
throws java.io.InvalidObjectException
java.io.InvalidObjectException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||