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

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

extends AbstractCollection<E extends @Nullable Object>
implements Multiset<E extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
com.google.common.collect.Multiset, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
com.google.common.collect.AbstractSortedMultiset, com.google.common.collect.ConcurrentHashMultiset, com.google.common.collect.EnumMultiset, com.google.common.collect.Multimaps.Keys, com.google.common.collect.Multisets.ViewMultiset, com.google.common.collect.AbstractMapBasedMultiset
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.collect.Multisets.setCountImpl

This class provides a skeletal implementation of the Multiset interface. A new multiset implementation can be created easily by extending this class and implementing the Multiset#entrySet() method, plus optionally overriding add(Object, int) and remove(Object, int) to enable modifications to the multiset.

The count and size implementations all iterate across the set returned by Multiset#entrySet(), as do many methods acting on the set returned by elementSet(). Override those methods for better performance.

Authors
Kevin Bourrillion, Louis Wasserman

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
pack-priv class

Field Summary

Modifier and TypeField and Description
private transient Set<E>
private transient Set<Multiset.Entry<E>>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public final boolean
add(E
the element to add one occurrence of; may be null only if explicitly allowed by the implementation
element
)

Overrides java.util.AbstractCollection.add.

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

Adds a single occurrence of the specified element to this multiset.
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
)

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

Adds a number of occurrences of an element to this multiset.
public final boolean
addAll(Collection<? extends E>
collection containing elements to be added to this collection
elementsToAdd
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Collection.addAll.

Adds all of the elements in the specified collection to this collection (optional operation).
public abstract void
clear()

Redeclares java.util.Collection.clear.

Overrides java.util.AbstractCollection.clear.

Removes all of the elements from this collection (optional operation).
public boolean
contains(Object
the element to check for
element
)

Overrides java.util.AbstractCollection.contains.

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

Determines whether this multiset contains the specified element.
pack-priv Set<E>
createElementSet()

Creates a new instance of this multiset's element set, which will be returned by elementSet().

pack-priv Set<Multiset.Entry<E>>
pack-priv abstract int
pack-priv abstract Iterator<E>
public Set<E>
elementSet()

Implements com.google.common.collect.Multiset.elementSet.

Returns the set of distinct elements contained in this multiset.
pack-priv abstract Iterator<Multiset.Entry<E>>
public Set<Multiset.Entry<E>>
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 final boolean
equals(Object
object to be compared for equality with this collection
object
)

Overrides java.lang.Object.equals.

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

Compares the specified object with this multiset for equality.
public final int
hashCode()

Overrides java.lang.Object.hashCode.

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

Returns the hash code for this multiset.
public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this collection contains no elements.
public final boolean
remove(Object
the element to remove one occurrence of
element
)

Overrides java.util.AbstractCollection.remove.

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

Removes a single occurrence of the specified element from this multiset, if present.
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
)

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

Removes a number of occurrences of the specified element from this multiset.
public final boolean
removeAll(Collection<?>
collection containing elements to be removed from this collection
elementsToRemove
)

Overrides java.util.AbstractCollection.removeAll.

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

Removes all of this collection's elements that are also contained in the specified collection (optional operation).
public final boolean
retainAll(Collection<?>
collection containing elements to be retained in this collection
elementsToRetain
)

Overrides java.util.AbstractCollection.retainAll.

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

Retains only the elements in this collection that are contained in the specified collection (optional operation).
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
)

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 boolean
setCount(E
the element to conditionally set the count of; may be null only if explicitly allowed by the implementation
element
,
int
the expected present count of the element in this multiset
oldCount
,
int
the desired count of the element in this multiset
newCount
)

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

Conditionally sets the count of an element to a new value, as described in setCount(Object, int), provided that the element has the expected current count.
public final String
toString()

Overrides java.util.AbstractCollection.toString.

Implements com.google.common.collect.Multiset.toString.

It is recommended, though not mandatory, that this method return the result of invoking toString on the entrySet, yielding a result such as [a x 3, c, d x 2, e].

Inherited from java.util.AbstractCollection:
containsAlliteratorsizetoArraytoArray

Field Detail

elementSetback to summary
private transient Set<E> elementSet
Annotations
@LazyInit
@CheckForNull
entrySetback to summary
private transient Set<Multiset.Entry<E>> entrySet
Annotations
@LazyInit
@CheckForNull

Constructor Detail

AbstractMultisetback to summary
pack-priv AbstractMultiset()

Method Detail

addback to summary
public final boolean add(E element)

Overrides java.util.AbstractCollection.add.

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

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

Adds a single occurrence of the specified element to this multiset.

This method refines Collection#add, which only ensures the presence of the element, to further specify that a successful call must always increment the count of the element, and the overall size of the collection, by one.

To both add the element and obtain the previous count of that element, use add(element, 1) instead.

Parameters
element:E

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

Returns:boolean

true always, since this call is required to modify the multiset, unlike other Collection types

Annotations
@CanIgnoreReturnValue
@Override
addback to summary
public int add(E element, int occurrences)

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
addAllback to summary
public final boolean addAll(Collection<? extends E> elementsToAdd)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Collection.addAll.

Doc from java.util.Collection.addAll.

Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.) If the specified collection has a defined encounter order, processing of its elements generally occurs in that order.

This implementation is highly efficient when elementsToAdd is itself a Multiset.

Parameters
elementsToAdd:Collection<? extends E>

collection containing elements to be added to this collection

Returns:boolean

true if this collection changed as a result of the call

Annotations
@CanIgnoreReturnValue
@Override
clearback to summary
public abstract void clear()

Redeclares java.util.Collection.clear.

Overrides java.util.AbstractCollection.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
containsback to summary
public boolean contains(Object element)

Overrides java.util.AbstractCollection.contains.

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

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

Determines whether this multiset contains the specified element.

This method refines Collection#contains to further specify that it may not throw an exception in response to element being null or of the wrong type.

Parameters
element:Object

the element to check for

Returns:boolean

true if this multiset contains at least one occurrence of the element

Annotations
@Override
createElementSetback to summary
pack-priv Set<E> createElementSet()

Creates a new instance of this multiset's element set, which will be returned by elementSet().

createEntrySetback to summary
pack-priv Set<Multiset.Entry<E>> createEntrySet()
distinctElementsback to summary
pack-priv abstract int distinctElements()
elementIteratorback to summary
pack-priv abstract Iterator<E> elementIterator()
elementSetback to summary
public Set<E> elementSet()

Implements com.google.common.collect.Multiset.elementSet.

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

Returns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.

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

Returns:Set<E>

a view of the set of distinct elements in this multiset

Annotations
@Override
entryIteratorback to summary
pack-priv abstract Iterator<Multiset.Entry<E>> entryIterator()
entrySetback to summary
public Set<Multiset.Entry<E>> 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.

Returns:Set<Multiset.Entry<E>>

a set of entries representing the data of this multiset

Annotations
@Override
equalsback to summary
public final boolean equals(Object object)

Overrides java.lang.Object.equals.

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

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

Compares the specified object with this multiset for equality. Returns true if the given object is also a multiset and contains equal elements with equal counts, regardless of order.

This implementation returns true if object is a multiset of the same size and if, for each element, the two multisets have the same count.

Parameters
object:Object

Doc from java.util.Collection.equals.

object to be compared for equality with this collection

Returns:boolean

Doc from java.util.Collection.equals.

true if the specified object is equal to this collection

Annotations
@Override
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

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

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

Returns the hash code for this multiset. This is defined as the sum of

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

This implementation returns the hash code of Multiset#entrySet().

Returns:int

Doc from java.util.Collection.hashCode.

the hash code value for this collection

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Doc from java.util.Collection.isEmpty.

Returns true if this collection contains no elements.

Returns:boolean

true if this collection contains no elements

Annotations
@Override
removeback to summary
public final boolean remove(Object element)

Overrides java.util.AbstractCollection.remove.

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

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

Removes a single occurrence of the specified element from this multiset, if present.

This method refines Collection#remove to further specify that it may not throw an exception in response to element being null or of the wrong type.

To both remove the element and obtain the previous count of that element, use remove(element, 1) instead.

Parameters
element:Object

the element to remove one occurrence of

Returns:boolean

true if an occurrence was found and removed

Annotations
@CanIgnoreReturnValue
@Override
removeback to summary
public int remove(Object element, int occurrences)

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
removeAllback to summary
public final boolean removeAll(Collection<?> elementsToRemove)

Overrides java.util.AbstractCollection.removeAll.

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

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

Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.

Note

This method ignores how often any element might appear in c, and only cares whether or not an element appears at all. If you wish to remove one occurrence in this multiset for every occurrence in c, see Multisets#removeOccurrences(Multiset, Multiset).

This method refines Collection#removeAll to further specify that it may not throw an exception in response to any of elements being null or of the wrong type.

Parameters
elementsToRemove:Collection<?>

Doc from java.util.Collection.removeAll.

collection containing elements to be removed from this collection

Returns:boolean

Doc from java.util.Collection.removeAll.

true if this collection changed as a result of the call

Annotations
@CanIgnoreReturnValue
@Override
retainAllback to summary
public final boolean retainAll(Collection<?> elementsToRetain)

Overrides java.util.AbstractCollection.retainAll.

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

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

Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.

Note

This method ignores how often any element might appear in c, and only cares whether or not an element appears at all. If you wish to remove one occurrence in this multiset for every occurrence in c, see Multisets#retainOccurrences(Multiset, Multiset).

This method refines Collection#retainAll to further specify that it may not throw an exception in response to any of elements being null or of the wrong type.

Parameters
elementsToRetain:Collection<?>

Doc from java.util.Collection.retainAll.

collection containing elements to be retained in this collection

Returns:boolean

Doc from java.util.Collection.retainAll.

true if this collection changed as a result of the call

Annotations
@CanIgnoreReturnValue
@Override
setCountback to summary
public int setCount(E element, int count)

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
setCountback to summary
public boolean setCount(E element, int oldCount, int newCount)

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

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

Conditionally sets the count of an element to a new value, as described in setCount(Object, int), provided that the element has the expected current count. If the current count is not oldCount, no change is made.

Parameters
element:E

the element to conditionally set the count of; may be null only if explicitly allowed by the implementation

oldCount:int

the expected present count of the element in this multiset

newCount:int

the desired count of the element in this multiset

Returns:boolean

true if the condition for modification was met. This implies that the multiset was indeed modified, unless oldCount == newCount.

Annotations
@CanIgnoreReturnValue
@Override
toStringback to summary
public final String toString()

Overrides java.util.AbstractCollection.toString.

Implements com.google.common.collect.Multiset.toString.

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

It is recommended, though not mandatory, that this method return the result of invoking toString on the entrySet, yielding a result such as [a x 3, c, d x 2, e].

This implementation returns the result of invoking toString on Multiset#entrySet().

Returns:String

Doc from java.util.AbstractCollection.toString.

a string representation of this collection

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

pack-priv Class AbstractMultiset.ElementSet

extends Multisets.ElementSet<E extends @Nullable Object>
Class Inheritance
Annotations
@WeakOuter

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Iterator<E>
iterator()

Implements abstract com.google.common.collect.Multisets.ElementSet.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.
pack-priv Multiset<E>
Inherited from com.google.common.collect.Multisets.ElementSet:
clearcontainscontainsAllisEmptyremovesize

Constructor Detail

ElementSetback to summary
pack-priv ElementSet()

Method Detail

iteratorback to summary
public Iterator<E> iterator()

Implements abstract com.google.common.collect.Multisets.ElementSet.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Returns:Iterator<E>

an iterator over the elements in this set

Annotations
@Override
multisetback to summary
pack-priv Multiset<E> multiset()

Implements abstract com.google.common.collect.Multisets.ElementSet.multiset.

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

pack-priv Class AbstractMultiset.EntrySet

extends Multisets.EntrySet<E extends @Nullable Object>
Class Inheritance
Known Direct Subclasses
com.google.common.collect.ConcurrentHashMultiset.EntrySet
Annotations
@WeakOuter

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Iterator<Multiset.Entry<E>>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.
pack-priv Multiset<E>
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set (its cardinality).
Inherited from com.google.common.collect.Multisets.EntrySet:
clearcontainsremove

Constructor Detail

EntrySetback to summary
pack-priv EntrySet()

Method Detail

iteratorback to summary
public Iterator<Multiset.Entry<E>> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Returns:Iterator<Multiset.Entry<E>>

an iterator over the elements in this set

Annotations
@Override
multisetback to summary
pack-priv Multiset<E> multiset()

Implements abstract com.google.common.collect.Multisets.EntrySet.multiset.

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Doc from java.util.Set.size.

Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this set (its cardinality)

Annotations
@Override