Package io.javago.sync
Class Map<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.concurrent.ConcurrentHashMap<K,V>
io.javago.sync.Map<K,V>
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Implemented Interfaces:
Serializable
,ConcurrentMap<K,
,V> Map<K,
V>
The
Map
class implements Go's sync.Map
.
A thread-safe map that extends ConcurrentHashMap
and provides additional utility methods.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap
ConcurrentHashMap.KeySetView<K,
V> Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionMap()
Creates a new, empty map with the default initial capacity, load factor, and concurrency level.Map
(int initialCapacity) Creates a new, empty map with the specified initial capacity, and with the default load factor and concurrency level.Map
(int initialCapacity, float loadFactor) Creates a new, empty map with the specified initial capacity and load factor, and with the default concurrency level.Map
(int initialCapacity, float loadFactor, int concurrencyLevel) Creates a new, empty map with the specified initial capacity, load factor, and concurrency level.Creates a new map with the same mappings as the specified map. -
Method Summary
Modifier and TypeMethodDescriptionboolean
compareAndDelete
(K key, V value) Removes the entry for a key only if currently mapped to a given value.boolean
compareAndSwap
(K key, V oldValue, V newValue) Replaces the entry for a key only if currently mapped to a given value.void
Removes the mapping for a key from this map if it is present.Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.loadAndDelete
(K key) Removes the mapping for a key from this map if it is present and returns the associated value.loadOrStore
(K key, V value) If the specified key is not already associated with a value, associates it with the given value and returnsnull
, else returns the current value.void
range
(BiConsumer<? super K, ? super V> consumer) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.void
Associates the specified value with the specified key in this map.Associates the specified value with the specified key in this map and returns the previous value associated with the key, ornull
if there was no mapping for the key.Methods inherited from class java.util.concurrent.ConcurrentHashMap
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, get, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, put, putAll, putIfAbsent, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, remove, remove, replace, replace, replaceAll, search, searchEntries, searchKeys, searchValues, size, toString, values
Methods inherited from class java.util.AbstractMap
clone
-
Constructor Details
-
Map
public Map()Creates a new, empty map with the default initial capacity, load factor, and concurrency level. -
Map
public Map(int initialCapacity) Creates a new, empty map with the specified initial capacity, and with the default load factor and concurrency level.- Parameters:
initialCapacity
- the initial capacity. The implementation performs internal sizing to accommodate this many elements.
-
Map
Creates a new map with the same mappings as the specified map.- Parameters:
m
- the map whose mappings are to be placed in this map
-
Map
public Map(int initialCapacity, float loadFactor) Creates a new, empty map with the specified initial capacity and load factor, and with the default concurrency level.- Parameters:
initialCapacity
- the initial capacity. The implementation performs internal sizing to accommodate this many elements.loadFactor
- the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
-
Map
public Map(int initialCapacity, float loadFactor, int concurrencyLevel) Creates a new, empty map with the specified initial capacity, load factor, and concurrency level.- Parameters:
initialCapacity
- the initial capacity. The implementation performs internal sizing to accommodate this many elements.loadFactor
- the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.concurrencyLevel
- the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
-
-
Method Details
-
compareAndDelete
Removes the entry for a key only if currently mapped to a given value.- Parameters:
key
- the key whose associated value is to be removedvalue
- the value expected to be associated with the specified key- Returns:
true
if the value was removed
-
compareAndSwap
Replaces the entry for a key only if currently mapped to a given value.- Parameters:
key
- the key with which the specified value is associatedoldValue
- the value expected to be associated with the specified keynewValue
- the value to be associated with the specified key- Returns:
true
if the value was replaced
-
delete
Removes the mapping for a key from this map if it is present.- Parameters:
key
- the key whose mapping is to be removed from the map
-
load
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
null
if this map contains no mapping for the key
-
loadAndDelete
Removes the mapping for a key from this map if it is present and returns the associated value.- Parameters:
key
- the key whose mapping is to be removed from the map- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
-
loadOrStore
If the specified key is not already associated with a value, associates it with the given value and returnsnull
, else returns the current value.- Parameters:
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key
-
range
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.- Parameters:
consumer
- the action to be performed for each entry
-
store
Associates the specified value with the specified key in this map.- Parameters:
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key
-
swap
Associates the specified value with the specified key in this map and returns the previous value associated with the key, ornull
if there was no mapping for the key.- Parameters:
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
-