public final class ConcurrentHashMultiset<E> extends AbstractMultiset<E> implements java.io.Serializable
Multiset
operations (exceptions where noted). Null elements are not supported.
See the Guava User Guide article on Multiset
.
Modifier and Type | Class and Description |
---|---|
private class |
ConcurrentHashMultiset.EntrySet |
private static class |
ConcurrentHashMultiset.FieldSettersHolder |
AbstractMultiset.ElementSet
Multiset.Entry<E>
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> |
countMap
The number of occurrences of each element.
|
private static long |
serialVersionUID |
Constructor and Description |
---|
ConcurrentHashMultiset(java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> countMap) |
Modifier and Type | Method and Description |
---|---|
int |
add(E element,
int occurrences)
Adds a number of occurrences of the specified element to this multiset.
|
void |
clear() |
int |
count(java.lang.Object element)
Returns the number of occurrences of
element in this multiset. |
static <E> ConcurrentHashMultiset<E> |
create()
Creates a new, empty
ConcurrentHashMultiset using the default initial capacity, load
factor, and concurrency settings. |
static <E> ConcurrentHashMultiset<E> |
create(java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> countMap)
Creates a new, empty
ConcurrentHashMultiset using countMap as the internal
backing map. |
static <E> ConcurrentHashMultiset<E> |
create(java.lang.Iterable<? extends E> elements)
Creates a new
ConcurrentHashMultiset containing the specified elements, using the
default initial capacity, load factor, and concurrency settings. |
(package private) java.util.Set<E> |
createElementSet()
Creates a new instance of this multiset's element set, which will be returned by
AbstractMultiset.elementSet() . |
java.util.Set<Multiset.Entry<E>> |
createEntrySet()
Deprecated.
Internal method, use
AbstractMultiset.entrySet() . |
(package private) int |
distinctElements() |
(package private) java.util.Iterator<E> |
elementIterator() |
(package private) java.util.Iterator<Multiset.Entry<E>> |
entryIterator() |
boolean |
isEmpty() |
java.util.Iterator<E> |
iterator() |
private void |
readObject(java.io.ObjectInputStream stream) |
int |
remove(java.lang.Object element,
int occurrences)
Removes a number of occurrences of the specified element from this multiset.
|
boolean |
removeExactly(java.lang.Object element,
int occurrences)
Removes exactly the specified number of occurrences of
element , or makes no change if
this is not possible. |
int |
setCount(E element,
int count)
Adds or removes occurrences of
element such that the count(java.lang.Object) of the element
becomes count . |
boolean |
setCount(E element,
int expectedOldCount,
int newCount)
Sets the number of occurrences of
element to newCount , but only if the count is
currently expectedOldCount . |
int |
size()
Returns the total number of all occurrences of all elements in this multiset.
|
private java.util.List<E> |
snapshot() |
java.lang.Object[] |
toArray() |
<T> T[] |
toArray(T[] array) |
private void |
writeObject(java.io.ObjectOutputStream stream) |
add, addAll, contains, elementSet, entrySet, equals, hashCode, remove, removeAll, retainAll, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, forEach, forEachEntry, spliterator
private final transient java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> countMap
private static final long serialVersionUID
ConcurrentHashMultiset(java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> countMap)
public static <E> ConcurrentHashMultiset<E> create()
ConcurrentHashMultiset
using the default initial capacity, load
factor, and concurrency settings.public static <E> ConcurrentHashMultiset<E> create(java.lang.Iterable<? extends E> elements)
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
.
elements
- the elements that the multiset should containpublic static <E> ConcurrentHashMultiset<E> create(java.util.concurrent.ConcurrentMap<E,java.util.concurrent.atomic.AtomicInteger> countMap)
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.
countMap
- backing map for storing the elements in the multiset and their counts. It must
be empty.java.lang.IllegalArgumentException
- if countMap
is not emptypublic int count(@CheckForNull java.lang.Object element)
element
in this multiset.public int size()
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.
public java.lang.Object[] toArray()
public <T> T[] toArray(T[] array)
private java.util.List<E> snapshot()
public int add(E element, int occurrences)
add
in interface Multiset<E>
add
in class AbstractMultiset<E>
element
- the element to addoccurrences
- the number of occurrences to addjava.lang.IllegalArgumentException
- if occurrences
is negative, or if the resulting amount
would exceed Integer.MAX_VALUE
public int remove(@CheckForNull java.lang.Object element, int occurrences)
remove
in interface Multiset<E>
remove
in class AbstractMultiset<E>
element
- the element whose occurrences should be removedoccurrences
- the number of occurrences of the element to removejava.lang.IllegalArgumentException
- if occurrences
is negativepublic boolean removeExactly(@CheckForNull java.lang.Object element, int occurrences)
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
.
element
- the element to removeoccurrences
- the number of occurrences of element
to removetrue
if the removal was possible (including if occurrences
is zero)java.lang.IllegalArgumentException
- if occurrences
is negativepublic int setCount(E element, int count)
element
such that the count(java.lang.Object)
of the element
becomes count
.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 multisetelement
in the multiset before this calljava.lang.IllegalArgumentException
- if count
is negativepublic boolean setCount(E element, int expectedOldCount, int newCount)
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.setCount
in interface Multiset<E>
setCount
in class AbstractMultiset<E>
element
- the element to conditionally set the count of; may be null only if explicitly
allowed by the implementationexpectedOldCount
- the expected present count of the element in this multisetnewCount
- the desired count of the element in this multisettrue
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.java.lang.IllegalArgumentException
- if expectedOldCount
or newCount
is negativejava.util.Set<E> createElementSet()
AbstractMultiset
AbstractMultiset.elementSet()
.createElementSet
in class AbstractMultiset<E>
java.util.Iterator<E> elementIterator()
elementIterator
in class AbstractMultiset<E>
@Deprecated public java.util.Set<Multiset.Entry<E>> createEntrySet()
AbstractMultiset.entrySet()
.createEntrySet
in class AbstractMultiset<E>
int distinctElements()
distinctElements
in class AbstractMultiset<E>
public boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
isEmpty
in class AbstractMultiset<E>
java.util.Iterator<Multiset.Entry<E>> entryIterator()
entryIterator
in class AbstractMultiset<E>
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 void clear()
clear
in interface java.util.Collection<E>
clear
in class AbstractMultiset<E>
private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException