Top Description Inners Fields Constructors Methods
com.google.common.collect

pack-priv abstract Class AbstractMapBasedMultiset<E extends @Nullable Object>

extends AbstractMultiset<E extends @Nullable Object>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
com.google.common.collect.HashMultiset, com.google.common.collect.LinkedHashMultiset
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, com.google.common.collect.CollectPreconditions.checkNonnegative, .CollectPreconditions.checkRemove, java.util.Objects.requireNonNull

Basic implementation of Multiset<E> backed by an instance of Map<E, Count>.

For serialization to work, the subclass must specify explicit readObject and writeObject methods.

Author
Kevin Bourrillion

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private transient Map<E, Count>
private static final long
private transient long

Constructor Summary

AccessConstructor and Description
protected
AbstractMapBasedMultiset(Map<E, Count> backingMap)

Standard constructor.

Method Summary

Modifier and TypeMethod and Description
public int
add(E
the element to add occurrences of; may be null only if explicitly allowed by the implementation
element
,
int
the number of occurrences of the element to add. May be zero, in which case no change will be made.
occurrences
)

Overrides com.google.common.collect.AbstractMultiset.add.

Implements com.google.common.collect.Multiset.add.

Adds a number of occurrences of an element to this multiset.
public void
clear()

Implements abstract com.google.common.collect.AbstractMultiset.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this collection (optional operation).
public int
count(Object
the element to count occurrences of
element
)

Implements com.google.common.collect.Multiset.count.

Returns the number of occurrences of an element in this multiset (the count of the element).
pack-priv int
pack-priv Iterator<E>
pack-priv Iterator<Multiset.Entry<E>>
public Set<Multiset.Entry<E>>
entrySet()

Overrides com.google.common.collect.AbstractMultiset.entrySet.

Implements com.google.common.collect.Multiset.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.
public void
forEachEntry(ObjIntConsumer<? super E> action)

Overrides default com.google.common.collect.Multiset.forEachEntry.

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)

public Iterator<E>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements com.google.common.collect.Multiset.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this collection.
private void
public int
remove(Object
the element to conditionally remove occurrences of
element
,
int
the number of occurrences of the element to remove. May be zero, in which case no change will be made.
occurrences
)

Overrides com.google.common.collect.AbstractMultiset.remove.

Implements com.google.common.collect.Multiset.remove.

Removes a number of occurrences of the specified element from this multiset.
pack-priv void
setBackingMap(Map<E, Count> backingMap)

Used during deserialization only.

public int
setCount(E
the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation
element
,
int
the desired count of the element in this multiset
count
)

Overrides com.google.common.collect.AbstractMultiset.setCount.

Implements com.google.common.collect.Multiset.setCount.

Adds or removes the necessary occurrences of an element such that the element attains the desired count.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements com.google.common.collect.Multiset.size, java.util.Collection.size.

Returns the total number of all occurrences of all elements in this multiset.
Inherited from com.google.common.collect.AbstractMultiset:
addaddAllcontainscreateElementSetcreateEntrySetelementSetequalshashCodeisEmptyremoveremoveAllretainAllsetCounttoString

Field Detail

backingMapback to summary
private transient Map<E, Count> backingMap
serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@GwtIncompatible
@J2ktIncompatible
sizeback to summary
private transient long size

Constructor Detail

AbstractMapBasedMultisetback to summary
protected AbstractMapBasedMultiset(Map<E, Count> backingMap)

Standard constructor.

Method Detail

addback to summary
public int add(E element, int occurrences)

Overrides com.google.common.collect.AbstractMultiset.add.

Implements com.google.common.collect.Multiset.add.

Doc from com.google.common.collect.Multiset.add.

Adds a number of occurrences of an element to this multiset. Note that if occurrences == 1, this method has the identical effect to 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.

Parameters
element:E

the element to add occurrences of; may be null only if explicitly allowed by the implementation

occurrences:int

the number of occurrences of the element to add. May be zero, in which case no change will be made.

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
Exceptions
IllegalArgumentException:
if the call would result in more than Integer#MAX_VALUE occurrences of element in this multiset.
clearback to summary
public void clear()

Implements abstract com.google.common.collect.AbstractMultiset.clear.

Implements java.util.Collection.clear.

Doc from java.util.Collection.clear.

Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.

Annotations
@Override
countback to summary
public int count(Object element)

Implements com.google.common.collect.Multiset.count.

Doc from com.google.common.collect.Multiset.count.

Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object#equals-based multiset, this gives the same result as Collections#frequency (which would presumably perform more poorly).

Note

the utility method Iterables#frequency generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type.

Parameters
element:Object

the element to count occurrences of

Returns:int

the number of occurrences of the element in this multiset; possibly zero but never negative

Annotations
@Override
distinctElementsback to summary
pack-priv int distinctElements()

Implements abstract com.google.common.collect.AbstractMultiset.distinctElements.

Annotations
@Override
elementIteratorback to summary
pack-priv Iterator<E> elementIterator()

Implements abstract com.google.common.collect.AbstractMultiset.elementIterator.

Annotations
@Override
entryIteratorback to summary
pack-priv Iterator<Multiset.Entry<E>> entryIterator()

Implements abstract com.google.common.collect.AbstractMultiset.entryIterator.

Annotations
@Override
entrySetback to summary
public Set<Multiset.Entry<E>> entrySet()

Overrides com.google.common.collect.AbstractMultiset.entrySet.

Implements com.google.common.collect.Multiset.entrySet.

Doc from com.google.common.collect.Multiset.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. This set contains exactly one entry for each distinct element in the multiset (thus it always has the same size as the 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.

Returns:Set<Multiset.Entry<E>>

a set of entries representing the data of this multiset

Annotations
@Override
forEachEntryback to summary
public void forEachEntry(ObjIntConsumer<? super E> action)

Overrides default com.google.common.collect.Multiset.forEachEntry.

Doc from com.google.common.collect.Multiset.forEachEntry.

Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element. For some Multiset implementations, this may be more efficient than iterating over the entrySet() either explicitly or with entrySet().forEach(action).

Annotations
@Override
getAndSetback to summary
private static int getAndSet(Count i, int count)
iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements com.google.common.collect.Multiset.iterator, java.util.Collection.iterator.

Doc from com.google.common.collect.Multiset.iterator.

Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

Elements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.

Returns:Iterator<E>

Doc from java.util.Collection.iterator.

an Iterator over the elements in this collection

Annotations
@Override
readObjectNoDataback to summary
private void readObjectNoData() throws ObjectStreamException
Annotations
@GwtIncompatible
@J2ktIncompatible
removeback to summary
public int remove(Object element, int occurrences)

Overrides com.google.common.collect.AbstractMultiset.remove.

Implements com.google.common.collect.Multiset.remove.

Doc from com.google.common.collect.Multiset.remove.

Removes 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 if occurrences == 1, this is functionally equivalent to the call remove(element).

Parameters
element:Object

the element to conditionally remove occurrences of

occurrences:int

the number of occurrences of the element to remove. May be zero, in which case no change will be made.

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
setBackingMapback to summary
pack-priv void setBackingMap(Map<E, Count> backingMap)

Used during deserialization only. The backing map must be empty.

setCountback to summary
public int setCount(E element, int count)

Overrides com.google.common.collect.AbstractMultiset.setCount.

Implements com.google.common.collect.Multiset.setCount.

Doc from com.google.common.collect.Multiset.setCount.

Adds or removes the necessary occurrences of an element such that the element attains the desired count.

Parameters
element:E

the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation

count:int

the desired count of the element in this multiset

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements com.google.common.collect.Multiset.size, java.util.Collection.size.

Doc from com.google.common.collect.Multiset.size.

Returns 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().

Returns:int

Doc from java.util.Collection.size.

the number of elements in this collection

Annotations
@Override
com.google.common.collect back to summary

private Class AbstractMapBasedMultiset.MapBasedMultisetIterator

extends Object
implements Iterator<E extends @Nullable Object>
Class Inheritance
  • java.lang.Object
  • com.google.common.collect.AbstractMapBasedMultiset.MapBasedMultisetIterator
All Implemented Interfaces
java.util.Iterator

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv Map.Entry<E, Count>
pack-priv final Iterator<Map.Entry<E, Count>>
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

canRemoveback to summary
pack-priv boolean canRemove
currentEntryback to summary
pack-priv Map.Entry<E, Count> currentEntry
Annotations
@CheckForNull
entryIteratorback to summary
pack-priv final Iterator<Map.Entry<E, Count>> entryIterator
occurrencesLeftback to summary
pack-priv int occurrencesLeft

Constructor Detail

MapBasedMultisetIteratorback to summary
pack-priv MapBasedMultisetIterator()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Annotations
@Override
@ParametricNullness
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override