Class AbstractMapBasedMultiset<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- com.google.common.collect.AbstractMultiset<E>
-
- com.google.common.collect.AbstractMapBasedMultiset<E>
-
- All Implemented Interfaces:
Multiset<E>,java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>
- Direct Known Subclasses:
HashMultiset,LinkedHashMultiset
abstract class AbstractMapBasedMultiset<E> extends AbstractMultiset<E> implements java.io.Serializable
Basic implementation ofMultiset<E>backed by an instance ofMap<E, Count>.For serialization to work, the subclass must specify explicit
readObjectandwriteObjectmethods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classAbstractMapBasedMultiset.MapBasedMultisetIterator-
Nested classes/interfaces inherited from class com.google.common.collect.AbstractMultiset
AbstractMultiset.ElementSet, AbstractMultiset.EntrySet
-
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<E,Count>backingMapprivate static longserialVersionUIDprivate longsize
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractMapBasedMultiset(java.util.Map<E,Count> backingMap)Standard constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intadd(E element, int occurrences)Adds a number of occurrences of an element to this multiset.voidclear()intcount(java.lang.Object element)Returns the number of occurrences of an element in this multiset (the count of the element).(package private) intdistinctElements()(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 intoMultiset.Entryinstances, each providing an element of the multiset and the count of that element.voidforEachEntry(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 intgetAndSet(Count i, int count)java.util.Iterator<E>iterator()private voidreadObjectNoData()intremove(java.lang.Object element, int occurrences)Removes a number of occurrences of the specified element from this multiset.(package private) voidsetBackingMap(java.util.Map<E,Count> backingMap)Used during deserialization only.intsetCount(E element, int count)Adds or removes the necessary occurrences of an element such that the element attains the desired count.intsize()Returns the total number of all occurrences of all elements in this multiset.-
Methods inherited from class com.google.common.collect.AbstractMultiset
add, addAll, contains, createElementSet, createEntrySet, elementSet, equals, hashCode, isEmpty, remove, removeAll, retainAll, setCount, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray, toArray, toArray
-
Methods inherited from interface com.google.common.collect.Multiset
containsAll, forEach, spliterator
-
-
-
-
Field Detail
-
size
private transient long size
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
setBackingMap
void setBackingMap(java.util.Map<E,Count> backingMap)
Used during deserialization only. The backing map must be empty.
-
entrySet
public java.util.Set<Multiset.Entry<E>> entrySet()
Returns a view of the contents of this multiset, grouped intoMultiset.Entryinstances, 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 theMultiset.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
Entryinstances 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 theEntryinstances 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.
-
elementIterator
java.util.Iterator<E> elementIterator()
- Specified by:
elementIteratorin classAbstractMultiset<E>
-
entryIterator
java.util.Iterator<Multiset.Entry<E>> entryIterator()
- Specified by:
entryIteratorin classAbstractMultiset<E>
-
forEachEntry
public void forEachEntry(java.util.function.ObjIntConsumer<? super E> action)
Description copied from interface:MultisetRuns the specified action for each distinct element in this multiset, and the number of occurrences of that element. For someMultisetimplementations, this may be more efficient than iterating over theMultiset.entrySet()either explicitly or withentrySet().forEach(action).- Specified by:
forEachEntryin interfaceMultiset<E>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection<E>- Specified by:
clearin classAbstractMultiset<E>
-
distinctElements
int distinctElements()
- Specified by:
distinctElementsin classAbstractMultiset<E>
-
size
public int size()
Description copied from interface:MultisetReturns the total number of all occurrences of all elements in this multiset.Note: this method does not return the number of distinct elements in the multiset, which is given by
entrySet().size().
-
iterator
public java.util.Iterator<E> iterator()
Description copied from interface:MultisetElements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.
-
count
public int count(java.lang.Object element)
Description copied from interface:MultisetReturns the number of occurrences of an element in this multiset (the count of the element). Note that for anObject.equals(java.lang.Object)-based multiset, this gives the same result asCollections.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.
-
add
public int add(E element, int occurrences)
Adds a number of occurrences of an element to this multiset. Note that ifoccurrences == 1, this method has the identical effect toMultiset.add(Object). This method is functionally equivalent (except in the case of overflow) to the calladdAll(Collections.nCopies(element, occurrences)), which would presumably perform much more poorly.- Specified by:
addin interfaceMultiset<E>- Overrides:
addin classAbstractMultiset<E>- Parameters:
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.- Returns:
- the count of the element before the operation; possibly zero
- Throws:
java.lang.IllegalArgumentException- if the call would result in more thanInteger.MAX_VALUEoccurrences ofelementin this multiset.
-
remove
public int remove(java.lang.Object element, int occurrences)Description copied from interface:MultisetRemoves a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed. Note that ifoccurrences == 1, this is functionally equivalent to the callremove(element).- Specified by:
removein interfaceMultiset<E>- Overrides:
removein classAbstractMultiset<E>- Parameters:
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.- Returns:
- the count of the element before the operation; possibly zero
-
setCount
public int setCount(E element, int count)
Description copied from interface:MultisetAdds or removes the necessary occurrences of an element such that the element attains the desired count.- Specified by:
setCountin interfaceMultiset<E>- Overrides:
setCountin classAbstractMultiset<E>- Parameters:
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 multiset- Returns:
- the count of the element before the operation; possibly zero
-
getAndSet
private static int getAndSet(Count i, int count)
-
readObjectNoData
private void readObjectNoData() throws java.io.ObjectStreamException- Throws:
java.io.ObjectStreamException
-
-