Top Description Fields Constructors Methods
com.google.common.util.concurrent

public final Class AtomicLongMap<K>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtCompatible
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, java.util.Objects.requireNonNull

A map containing 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, 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 com.google.common.collect.Multiset such as com.google.common.collect.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.

Author
Charles Fry
Since
11.0

Field Summary

Modifier and TypeField and Description
private transient Map<K, Long>
private final ConcurrentHashMap<K, Long>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public long
accumulateAndGet(K key, long x, LongBinaryOperator accumulatorFunction)

Updates the value currently associated with key by combining it with x via the specified accumulator function, returning the new value.

public long
addAndGet(K key, long delta)

Adds delta to the value currently associated with key, and returns the new value.

public Map<K, Long>
asMap()

Returns a live, read-only view of the map backing this AtomicLongMap.

public void
clear()

Removes all of the mappings from this map.

public boolean
containsKey(Object key)

Returns true if this map contains a mapping for the specified key.

public static <K> AtomicLongMap<K>
create()

Creates an AtomicLongMap.

public static <K> AtomicLongMap<K>
create(Map<? extends K, ? extends Long> m)

Creates an AtomicLongMap with the same mappings as the specified Map.

private Map<K, Long>
public long
decrementAndGet(K key)

Decrements by one the value currently associated with key, and returns the new value.

public long
get(K key)

Returns the value associated with key, or zero if there is no value associated with key.

public long
getAndAccumulate(K key, long x, LongBinaryOperator accumulatorFunction)

Updates the value currently associated with key by combining it with x via the specified accumulator function, returning the old value.

public long
getAndAdd(K key, long delta)

Adds delta to the value currently associated with key, and returns the old value.

public long
getAndDecrement(K key)

Decrements by one the value currently associated with key, and returns the old value.

public long
getAndIncrement(K key)

Increments by one the value currently associated with key, and returns the old value.

public long
getAndUpdate(K key, LongUnaryOperator updaterFunction)

Updates the value currently associated with key with the specified function, and returns the old value.

public long
incrementAndGet(K key)

Increments by one the value currently associated with key, and returns the new value.

public boolean
isEmpty()

Returns true if this map contains no key-value mappings.

public long
put(K key, long newValue)

Associates newValue with key in this map, and returns the value previously associated with key, or zero if there was no such value.

public void
putAll(Map<? extends K, ? extends Long> m)

Copies all of the mappings from the specified map to this map.

pack-priv long
putIfAbsent(K key, long newValue)

If key is not already associated with a value or if key is associated with zero, associate it with newValue.

public long
remove(K key)

Removes and returns the value associated with key.

pack-priv boolean
remove(K key, long value)

If (key, value) is currently in the map, this method removes it and returns true; otherwise, this method returns false.

public void
removeAllZeros()

Removes all mappings from this map whose values are zero.

public boolean
removeIfZero(K key)

Atomically remove key from the map iff its associated value is 0.

pack-priv boolean
replace(K key, long expectedOldValue, long newValue)

If (key, expectedOldValue) is currently in the map, this method replaces expectedOldValue with newValue and returns true; otherwise, this method returns false.

public int
size()

Returns the number of key-value mappings in this map.

public long
sum()

Returns the sum of all values in this map.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public long
updateAndGet(K key, LongUnaryOperator updaterFunction)

Updates the value currently associated with key with the specified function, and returns the new value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

asMapback to summary
private transient Map<K, Long> asMap
Annotations
@LazyInit
@CheckForNull
mapback to summary
private final ConcurrentHashMap<K, Long> map

Constructor Detail

AtomicLongMapback to summary
private AtomicLongMap(ConcurrentHashMap<K, Long> map)

Method Detail

accumulateAndGetback to summary
public long accumulateAndGet(K key, long x, LongBinaryOperator accumulatorFunction)

Updates the value currently associated with key by combining it with x via the specified accumulator function, returning the new value. The previous value associated with key (or zero, if there is none) is passed as the first argument to accumulatorFunction, and x is passed as the second argument.

Annotations
@CanIgnoreReturnValue
Since
21.0
addAndGetback to summary
public long addAndGet(K key, long delta)

Adds delta to the value currently associated with key, and returns the new value.

Annotations
@CanIgnoreReturnValue
asMapback to summary
public Map<K, Long> asMap()

Returns a live, read-only view of the map backing this AtomicLongMap.

clearback to summary
public 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.

containsKeyback to summary
public boolean containsKey(Object key)

Returns true if this map contains a mapping for the specified key.

createback to summary
public static <K> AtomicLongMap<K> create()

Creates an AtomicLongMap.

createback to summary
public static <K> AtomicLongMap<K> create(Map<? extends K, ? extends Long> m)

Creates an AtomicLongMap with the same mappings as the specified Map.

createAsMapback to summary
private Map<K, Long> createAsMap()
decrementAndGetback to summary
public long decrementAndGet(K key)

Decrements by one the value currently associated with key, and returns the new value.

Annotations
@CanIgnoreReturnValue
getback to summary
public long get(K key)

Returns the value associated with key, or zero if there is no value associated with key.

getAndAccumulateback to summary
public long getAndAccumulate(K key, long x, LongBinaryOperator accumulatorFunction)

Updates the value currently associated with key by combining it with x via the specified accumulator function, returning the old value. The previous value associated with key (or zero, if there is none) is passed as the first argument to accumulatorFunction, and x is passed as the second argument.

Annotations
@CanIgnoreReturnValue
Since
21.0
getAndAddback to summary
public long getAndAdd(K key, long delta)

Adds delta to the value currently associated with key, and returns the old value.

Annotations
@CanIgnoreReturnValue
getAndDecrementback to summary
public long getAndDecrement(K key)

Decrements by one the value currently associated with key, and returns the old value.

Annotations
@CanIgnoreReturnValue
getAndIncrementback to summary
public long getAndIncrement(K key)

Increments by one the value currently associated with key, and returns the old value.

Annotations
@CanIgnoreReturnValue
getAndUpdateback to summary
public long getAndUpdate(K key, LongUnaryOperator updaterFunction)

Updates the value currently associated with key with the specified function, and returns the old value. If there is not currently a value associated with key, the function is applied to 0L.

Annotations
@CanIgnoreReturnValue
Since
21.0
incrementAndGetback to summary
public long incrementAndGet(K key)

Increments by one the value currently associated with key, and returns the new value.

Annotations
@CanIgnoreReturnValue
isEmptyback to summary
public boolean isEmpty()

Returns true if this map contains no key-value mappings.

putback to summary
public long put(K key, long newValue)

Associates newValue with key in this map, and returns the value previously associated with key, or zero if there was no such value.

Annotations
@CanIgnoreReturnValue
putAllback to summary
public void putAll(Map<? extends K, ? extends Long> m)

Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.

putIfAbsentback to summary
pack-priv long putIfAbsent(K key, long newValue)

If key is not already associated with a value or if key is associated with zero, associate it with newValue. Returns the previous value associated with key, or zero if there was no mapping for key.

removeback to summary
public long remove(K key)

Removes and returns the value associated with key. If key is not in the map, this method has no effect and returns zero.

Annotations
@CanIgnoreReturnValue
removeback to summary
pack-priv boolean remove(K key, long value)

If (key, value) is currently in the map, this method removes it and returns true; otherwise, this method returns false.

removeAllZerosback to summary
public 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.

removeIfZeroback to summary
public boolean removeIfZero(K key)

Atomically remove key from the map iff its associated value is 0.

Annotations
@CanIgnoreReturnValue
Since
20.0
replaceback to summary
pack-priv boolean replace(K key, long expectedOldValue, long newValue)

If (key, expectedOldValue) is currently in the map, this method replaces expectedOldValue with newValue and returns true; otherwise, this method returns false.

If expectedOldValue is zero, this method will succeed if (key, zero) is currently in the map, or if key is not in the map at all.

sizeback to summary
public int size()

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

sumback to summary
public 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.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
updateAndGetback to summary
public long updateAndGet(K key, LongUnaryOperator updaterFunction)

Updates the value currently associated with key with the specified function, and returns the new value. If there is not currently a value associated with key, the function is applied to 0L.

Annotations
@CanIgnoreReturnValue
Since
21.0