@GwtIncompatible 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. |
static <E> ConcurrentHashMultiset<E> |
create(MapMaker mapMaker)
Deprecated.
Use
create(ConcurrentMap) instead. This method is scheduled for deletion
in Guava 21.0. |
(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() |
(package private) int |
distinctElements() |
(package private) java.util.Iterator<Multiset.Entry<E>> |
entryIterator() |
boolean |
isEmpty() |
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() |
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, iterator, remove, removeAll, retainAll, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll
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 contain@Beta @Deprecated public static <E> ConcurrentHashMultiset<E> create(MapMaker mapMaker)
create(ConcurrentMap)
instead. This method is scheduled for deletion
in Guava 21.0.ConcurrentHashMultiset
using mapMaker
to construct the
internal backing map.
If this MapMaker
is configured to use entry eviction of any kind, this eviction
applies to all occurrences of a given element as a single unit. However, most updates to the
multiset do not count as map updates at all, since we're usually just mutating the value stored
in the map, so MapMaker#expireAfterAccess
makes sense (evict the entry that was queried
or updated longest ago), but MapMaker#expireAfterWrite
doesn't, because the eviction
time is measured from when we saw the first occurrence of the object.
The returned multiset is serializable but any serialization caveats given in MapMaker
apply.
Finally, soft/weak values can be used but are not very useful: the values are created internally and not exposed externally, so no one else will have a strong reference to the values. Weak keys on the other hand can be useful in some scenarios.
GenericMapMaker
class)
since 7.0)@Beta public 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(@Nullable java.lang.Object element)
element
in this multiset.public int 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.
size
in interface java.util.Collection<E>
size
in class AbstractMultiset<E>
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(@Nullable 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(@Nullable 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>
public java.util.Set<Multiset.Entry<E>> createEntrySet()
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 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