@GwtCompatible(emulated=true) abstract class AbstractMapBasedMultiset<E> extends AbstractMultiset<E> implements java.io.Serializable
Multiset<E>
backed by an instance of Map<E, Count>
.
For serialization to work, the subclass must specify explicit readObject
and writeObject
methods.
Modifier and Type | Class and Description |
---|---|
private class |
AbstractMapBasedMultiset.MapBasedMultisetIterator |
AbstractMultiset.ElementSet, AbstractMultiset.EntrySet
Multiset.Entry<E>
Modifier and Type | Field and Description |
---|---|
private java.util.Map<E,Count> |
backingMap |
private static long |
serialVersionUID |
private long |
size |
Modifier | Constructor and Description |
---|---|
protected |
AbstractMapBasedMultiset(java.util.Map<E,Count> backingMap)
Standard constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
add(E element,
int occurrences)
Adds a number of occurrences of an element to this multiset.
|
void |
clear() |
int |
count(java.lang.Object element)
Returns the number of occurrences of an element in this multiset (the count of the
element).
|
(package private) int |
distinctElements() |
(package private) java.util.Iterator<E> |
elementIterator() |
(package private) java.util.Iterator<Multiset.Entry<E>> |
entryIterator() |
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. |
void |
forEachEntry(java.util.function.ObjIntConsumer<? super E> action)
Runs the specified action for each distinct element in this multiset, and the number of
occurrences of that element.
|
private static int |
getAndSet(Count i,
int count) |
java.util.Iterator<E> |
iterator() |
private void |
readObjectNoData() |
int |
remove(java.lang.Object element,
int occurrences)
Removes a number of occurrences of the specified element from this multiset.
|
(package private) void |
setBackingMap(java.util.Map<E,Count> backingMap)
Used during deserialization only.
|
int |
setCount(E element,
int count)
Adds or removes the necessary occurrences of an element such that the element attains the
desired count.
|
int |
size()
Returns the total number of all occurrences of all elements in this multiset.
|
add, addAll, contains, createElementSet, createEntrySet, elementSet, equals, hashCode, isEmpty, remove, removeAll, retainAll, setCount, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, forEach, spliterator
private transient long size
@GwtIncompatible private static final long serialVersionUID
void setBackingMap(java.util.Map<E,Count> 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.
java.util.Iterator<E> elementIterator()
elementIterator
in class AbstractMultiset<E>
java.util.Iterator<Multiset.Entry<E>> entryIterator()
entryIterator
in class AbstractMultiset<E>
public void forEachEntry(java.util.function.ObjIntConsumer<? super E> action)
Multiset
Multiset
implementations, this may be more
efficient than iterating over the Multiset.entrySet()
either explicitly or with entrySet().forEach(action)
.forEachEntry
in interface Multiset<E>
public void clear()
clear
in interface java.util.Collection<E>
clear
in class AbstractMultiset<E>
int distinctElements()
distinctElements
in class AbstractMultiset<E>
public int size()
Multiset
Note: this method does not return the number of distinct elements in the
multiset, which is given by entrySet().size()
.
public java.util.Iterator<E> iterator()
Multiset
Elements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.
public int count(java.lang.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.
public int add(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.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 the 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(java.lang.Object element, int occurrences)
Multiset
occurrences == 1
, this is functionally equivalent to the call remove(element)
.remove
in interface Multiset<E>
remove
in class AbstractMultiset<E>
element
- the element to conditionally remove occurrences ofoccurrences
- the number of occurrences of the element to remove. May be zero, in which
case no change will be made.public int setCount(E element, int count)
Multiset
setCount
in interface Multiset<E>
setCount
in class AbstractMultiset<E>
element
- the element to add or remove occurrences of; may be null only if explicitly
allowed by the implementationcount
- the desired count of the element in this multisetprivate static int getAndSet(Count i, int count)
@GwtIncompatible private void readObjectNoData() throws java.io.ObjectStreamException
java.io.ObjectStreamException