Class AtomicLongMap<K>
- All Implemented Interfaces:
- Serializable
long values that can be atomically updated. While writes to a
 traditional Map rely on put(K, V), the typical mechanism for writing to this map
 is addAndGet(K, long), which adds a long to the value currently associated with
 K. If a key has not yet been associated with a value, its implicit value is zero.
 Most methods in this class treat absent values and zero values identically, as individually
 documented. Exceptions to this are containsKey(java.lang.Object), size(), isEmpty(), asMap, and toString().
 
Instances of this class may be used by multiple threads concurrently. All operations are atomic unless otherwise noted.
Instances of this class are serializable if the keys are serializable.
Note: If your values are always positive and less than 2^31, you may wish to use a
 Multiset such as ConcurrentHashMultiset instead.
 
Warning: Unlike Multiset, entries whose values are zero are not automatically
 removed from the map. Instead they must be removed manually with removeAllZeros().
- Since:
- 11.0
- Author:
- Charles Fry
- See Also:
- 
Method SummaryModifier and TypeMethodDescriptionlongAddsdeltato the value currently associated withkey, and returns the new value.asMap()Returns a live, read-only view of the map backing thisAtomicLongMap.voidclear()Removes all of the mappings from this map.booleancontainsKey(Object key) Returns true if this map contains a mapping for the specified key.static <K> AtomicLongMap<K> create()Creates anAtomicLongMap.static <K> AtomicLongMap<K> Creates anAtomicLongMapwith the same mappings as the specifiedMap.longdecrementAndGet(K key) Decrements by one the value currently associated withkey, and returns the new value.longReturns the value associated withkey, or zero if there is no value associated withkey.longAddsdeltato the value currently associated withkey, and returns the old value.longgetAndDecrement(K key) Decrements by one the value currently associated withkey, and returns the old value.longgetAndIncrement(K key) Increments by one the value currently associated withkey, and returns the old value.longincrementAndGet(K key) Increments by one the value currently associated withkey, and returns the new value.booleanisEmpty()Returnstrueif this map contains no key-value mappings.longAssociatesnewValuewithkeyin this map, and returns the value previously associated withkey, or zero if there was no such value.voidCopies all of the mappings from the specified map to this map.longRemoves and returns the value associated withkey.voidRemoves all mappings from this map whose values are zero.booleanremoveIfZero(K key) Atomically removekeyfrom the map iff its associated value is 0.intsize()Returns the number of key-value mappings in this map.longsum()Returns the sum of all values in this map.toString()
- 
Method Details- 
createCreates anAtomicLongMap.
- 
createCreates anAtomicLongMapwith the same mappings as the specifiedMap.
- 
getReturns the value associated withkey, or zero if there is no value associated withkey.
- 
incrementAndGetIncrements by one the value currently associated withkey, and returns the new value.
- 
decrementAndGetDecrements by one the value currently associated withkey, and returns the new value.
- 
addAndGetAddsdeltato the value currently associated withkey, and returns the new value.
- 
getAndIncrementIncrements by one the value currently associated withkey, and returns the old value.
- 
getAndDecrementDecrements by one the value currently associated withkey, and returns the old value.
- 
getAndAddAddsdeltato the value currently associated withkey, and returns the old value.
- 
putAssociatesnewValuewithkeyin this map, and returns the value previously associated withkey, or zero if there was no such value.
- 
putAllCopies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of callingput(k, v)on this map once for each mapping from keykto valuevin the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
- 
removeRemoves and returns the value associated withkey. Ifkeyis not in the map, this method has no effect and returns zero.
- 
removeIfZeroAtomically removekeyfrom the map iff its associated value is 0.- Since:
- 20.0
 
- 
removeAllZerospublic void removeAllZeros()Removes all mappings from this map whose values are zero.This method is not atomic: the map may be visible in intermediate states, where some of the zero values have been removed and others have not. 
- 
sumpublic long sum()Returns the sum of all values in this map.This method is not atomic: the sum may or may not include other concurrent operations. 
- 
asMap
- 
containsKeyReturns true if this map contains a mapping for the specified key.
- 
sizepublic int size()Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.
- 
isEmptypublic boolean isEmpty()Returnstrueif this map contains no key-value mappings.
- 
clearpublic void clear()Removes all of the mappings from this map. The map will be empty after this call returns.This method is not atomic: the map may not be empty after returning if there were concurrent writes. 
- 
toString
 
-