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

public final Class ConcurrentHashMultiset<E>

extends AbstractMultiset<E>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, com.google.common.collect.CollectPreconditions.checkNonnegative

A multiset that supports concurrent modifications and that provides atomic versions of most Multiset operations (exceptions where noted). Null elements are not supported.

See the Guava User Guide article on Multiset.

Authors
Cliff L. Biffle, mike nonemacher
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class

Field Summary

Modifier and TypeField and Description
private final transient ConcurrentMap<E, AtomicInteger>
countMap

The number of occurrences of each element.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the previous count of the element before the operation; possibly zero
add
(E
the element to add
element
,
int
the number of occurrences to add
occurrences
)

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

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

Adds a number of occurrences of the specified 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

Returns:

the nonnegative number of occurrences of the element
count
(Object
the element to look for
element
)

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

Returns the number of occurrences of element in this multiset.
public static <E> ConcurrentHashMultiset<E>
create()

Creates a new, empty ConcurrentHashMultiset using the default initial capacity, load factor, and concurrency settings.

public static <E> ConcurrentHashMultiset<E>
create(Iterable<? extends E>
the elements that the multiset should contain
elements
)

Creates a new ConcurrentHashMultiset containing the specified elements, using the default initial capacity, load factor, and concurrency settings.

public static <E> ConcurrentHashMultiset<E>
create(ConcurrentMap<E, AtomicInteger>
backing map for storing the elements in the multiset and their counts. It must be empty.
countMap
)

Creates a new, empty ConcurrentHashMultiset using countMap as the internal backing map.

pack-priv Set<E>
createElementSet()

Overrides com.google.common.collect.AbstractMultiset.createElementSet.

Creates a new instance of this multiset's element set, which will be returned by elementSet().
public Set<Multiset.Entry<E>>
pack-priv int
pack-priv Iterator<E>
pack-priv Iterator<Multiset.Entry<E>>
public boolean
isEmpty()

Overrides com.google.common.collect.AbstractMultiset.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this collection contains no elements.
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

Returns:

the count of the element before the operation; possibly zero
remove
(Object
the element whose occurrences should be removed
element
,
int
the number of occurrences of the element to remove
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.
public boolean

Returns:

true if the removal was possible (including if occurrences is zero)
removeExactly
(Object
the element to remove
element
,
int
the number of occurrences of element to remove
occurrences
)

Removes exactly the specified number of occurrences of element, or makes no change if this is not possible.

public int

Returns:

the count of element in the multiset before this call
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 occurrences of element such that the count of the element becomes count.
public boolean

Returns:

true if the change was successful. This usually indicates that the multiset has been modified, but not always: in the case that expectedOldCount == newCount, the method will return true if the condition was met.
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
expectedOldCount
,
int
the desired count of the element in this multiset
newCount
)

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

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

Sets the number of occurrences of element to newCount, but only if the count is currently expectedOldCount.
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.
private List<E>
public Object[]
toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this collection.
public <T extends @Nullable Object> T[]
toArray(T[]
the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
array
)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
private void
Inherited from com.google.common.collect.AbstractMultiset:
addaddAllcontainselementSetentrySetequalshashCoderemoveremoveAllretainAlltoString

Field Detail

countMapback to summary
private final transient ConcurrentMap<E, AtomicInteger> countMap

The number of occurrences of each element.

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ConcurrentHashMultisetback to summary
pack-priv ConcurrentHashMultiset(ConcurrentMap<E, AtomicInteger> countMap)
Annotations
@VisibleForTesting

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.

Adds a number of occurrences of the specified element to this multiset.

Parameters
element:E

the element to add

occurrences:int

the number of occurrences to add

Returns:int

the previous count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
Exceptions
IllegalArgumentException:
if occurrences is negative, or if the resulting amount would exceed Integer#MAX_VALUE
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.

Returns the number of occurrences of element in this multiset.

Parameters
element:Object

the element to look for

Returns:int

the nonnegative number of occurrences of the element

Annotations
@Override
createback to summary
public static <E> ConcurrentHashMultiset<E> create()

Creates a new, empty ConcurrentHashMultiset using the default initial capacity, load factor, and concurrency settings.

createback to summary
public static <E> ConcurrentHashMultiset<E> create(Iterable<? extends E> elements)

Creates a new ConcurrentHashMultiset containing the specified elements, using the default initial capacity, load factor, and concurrency settings.

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

Parameters
elements:Iterable<? extends E>

the elements that the multiset should contain

createback to summary
public static <E> ConcurrentHashMultiset<E> create(ConcurrentMap<E, AtomicInteger> countMap)

Creates a new, empty ConcurrentHashMultiset using countMap as the internal backing map.

This instance will assume ownership of countMap, and other code should not maintain references to the map or modify it in any way.

The returned multiset is serializable if the input map is.

Parameters
countMap:ConcurrentMap<E, AtomicInteger>

backing map for storing the elements in the multiset and their counts. It must be empty.

Exceptions
IllegalArgumentException:
if countMap is not empty
Since
20.0
createElementSetback to summary
pack-priv Set<E> createElementSet()

Overrides com.google.common.collect.AbstractMultiset.createElementSet.

Doc from com.google.common.collect.AbstractMultiset.createElementSet.

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

Annotations
@Override
createEntrySetback to summary
public Set<Multiset.Entry<E>> createEntrySet()

Overrides com.google.common.collect.AbstractMultiset.createEntrySet.

Deprecated

Internal method, use entrySet().

Annotations
@Deprecated
@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
isEmptyback to summary
public boolean isEmpty()

Overrides com.google.common.collect.AbstractMultiset.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
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
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
Annotations
@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.

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.

Parameters
element:Object

the element whose occurrences should be removed

occurrences:int

the number of occurrences of the element to remove

Returns:int

the count of the element before the operation; possibly zero

Annotations
@CanIgnoreReturnValue
@Override
Exceptions
IllegalArgumentException:
if occurrences is negative
removeExactlyback to summary
public boolean removeExactly(Object element, int occurrences)

Removes exactly the specified number of occurrences of element, or makes no change if this is not possible.

This method, in contrast to remove(Object, int), has no effect when the element count is smaller than occurrences.

Parameters
element:Object

the element to remove

occurrences:int

the number of occurrences of element to remove

Returns:boolean

true if the removal was possible (including if occurrences is zero)

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if occurrences is negative
setCountback to summary
public int setCount(E element, int count)

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

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

Adds or removes occurrences of element such that the count of the element becomes count.

Parameters
element:E

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

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

count:int

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

the desired count of the element in this multiset

Returns:int

the count of element in the multiset before this call

Annotations
@CanIgnoreReturnValue
@Override
Exceptions
IllegalArgumentException:
if count is negative
setCountback to summary
public boolean setCount(E element, int expectedOldCount, int newCount)

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

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

Sets the number of occurrences of element to newCount, but only if the count is currently expectedOldCount. If element does not appear in the multiset exactly expectedOldCount times, no changes will be made.

Parameters
element:E

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

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

expectedOldCount:int

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

the expected present count of the element in this multiset

newCount:int

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

the desired count of the element in this multiset

Returns:boolean

true if the change was successful. This usually indicates that the multiset has been modified, but not always: in the case that expectedOldCount == newCount, the method will return true if the condition was met.

Annotations
@CanIgnoreReturnValue
@Override
Exceptions
IllegalArgumentException:
if expectedOldCount or newCount is negative
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().

If the data in the multiset is modified by any other threads during this method, it is undefined which (if any) of these modifications will be reflected in the result.

Returns:int

Doc from java.util.Collection.size.

the number of elements in this collection

Annotations
@Override
snapshotback to summary
private List<E> snapshot()
toArrayback to summary
public Object[] toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Doc from java.util.Collection.toArray.

Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is Object.

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

Returns:Object[]

an array, whose runtime component type is Object, containing all of the elements in this collection

Annotations
@Override
toArrayback to summary
public <T extends @Nullable Object> T[] toArray(T[] array)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Doc from java.util.Collection.toArray.

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

Parameters
<T>
the component type of the array to contain the collection
array:T[]

the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Returns:T[]

an array containing all of the elements in this collection

Annotations
@Override
@SuppressWarnings:nullness
writeObjectback to summary
private void writeObject(ObjectOutputStream stream) throws IOException
Serial data
the ConcurrentMap of elements and their counts.
com.google.common.collect back to summary

private Class ConcurrentHashMultiset.EntrySet

extends AbstractMultiset<E>.EntrySet
Class Inheritance
Annotations
@WeakOuter

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv ConcurrentHashMultiset<E>
private List<Multiset.Entry<E>>
public Object[]
toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Returns an array containing all of the elements in this set.
public <T extends @Nullable Object> T[]
toArray(T[]
the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
array
)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
Inherited from com.google.common.collect.AbstractMultiset.EntrySet:
iteratorsize

Constructor Detail

EntrySetback to summary
private EntrySet()

Method Detail

multisetback to summary
pack-priv ConcurrentHashMultiset<E> multiset()

Overrides com.google.common.collect.AbstractMultiset.EntrySet.multiset.

Annotations
@Override
snapshotback to summary
private List<Multiset.Entry<E>> snapshot()
toArrayback to summary
public Object[] toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Doc from java.util.Set.toArray.

Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

The returned array will be "safe" in that no references to it are maintained by this set. (In other words, this method must allocate a new array even if this set is backed by an array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Returns:Object[]

an array containing all the elements in this set

Annotations
@Override
toArrayback to summary
public <T extends @Nullable Object> T[] toArray(T[] array)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Doc from java.util.Set.toArray.

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set.

If this set fits in the specified array with room to spare (i.e., the array has more elements than this set), the element in the array immediately following the end of the set is set to null. (This is useful in determining the length of this set only if the caller knows that this set does not contain any null elements.)

If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a set known to contain only strings. The following code can be used to dump the set into a newly allocated array of String:

    String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
Parameters
<T>

Doc from java.util.Collection.toArray. the component type of the array to contain the collection

array:T[]

the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Returns:T[]

an array containing all the elements in this set

Annotations
@Override
@SuppressWarnings:nullness
com.google.common.collect back to summary

private Class ConcurrentHashMultiset.FieldSettersHolder

extends Object
Class Inheritance
  • java.lang.Object
  • com.google.common.collect.ConcurrentHashMultiset.FieldSettersHolder

Field Summary

Modifier and TypeField and Description
pack-priv static final Serialization.FieldSetter<ConcurrentHashMultiset<E>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COUNT_MAP_FIELD_SETTERback to summary
pack-priv static final Serialization.FieldSetter<ConcurrentHashMultiset<E>> COUNT_MAP_FIELD_SETTER

Constructor Detail

FieldSettersHolderback to summary
private FieldSettersHolder()