Class ConcurrentHashMap<K,V>
- All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K, V>
java.util.concurrent.ConcurrentHashMap for the Codename One runtime,
over a HashMap.
It is safe to use from several threads at once. Every operation holds
the map's monitor, so the compound operations (putIfAbsent,
computeIfAbsent, compute, merge, ...) are atomic, and a mapping
function runs while the monitor is held: it must not touch the map. Two
threads never read and write at the same moment, which a device's few
threads rarely notice and which keeps the class small.
It also keeps from the JDK class:
nullkeys and values are rejected withNullPointerException.- Iterators never throw
ConcurrentModificationException. Each iterates over the entries present when it was created, so a loop may add to and remove from the map it is walking;Iterator.removeandMap.Entry.setValuewrite through to the map.
The bulk parallel operations (forEach(long, ...), search, reduce)
and forEachKey/forEachValue are not provided.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classjava.util.concurrent.ConcurrentHashMap.KeySetView: the keys of a map as a set.Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentHashMap(int initialCapacity) ConcurrentHashMap(int initialCapacity, float loadFactor) ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) ConcurrentHashMap(Map<? extends K, ? extends V> m) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all elements from this map, leaving it empty.Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) If the specified key is not already associated with a value (or is mapped tonull), attempts to compute its value using the given mapping function and enters it into this map unlessnull.computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.booleanbooleancontainsKey(Object key) Returns whether this map contains the specified key.booleancontainsValue(Object value) Returns whether this map contains the specified value.elements()entrySet()Returns a set containing all of the mappings in this map.voidforEach(BiConsumer<? super K, ? super V> action) Performs the given action for each entry in this map.Returns the value of the mapping with the specified key.getOrDefault(Object key, V defaultValue) Returns the value to which the specified key is mapped, ordefaultValueif this map contains no mapping for the key.booleanisEmpty()Returns whether this map is empty.keys()keySet()The keys, as a view of this map.longIf the specified key is not already associated with a value or is associated withnull, associates it with the given non-null value.static <K> ConcurrentHashMap.KeySetView<K, Boolean> static <K> ConcurrentHashMap.KeySetView<K, Boolean> newKeySet(int initialCapacity) Maps the specified key to the specified value.voidCopies every mapping in the specified map to this map.putIfAbsent(K key, V value) If the specified key is not already associated with a value (or is mapped tonull) associates it with the given value and returnsnull, else returns the current value.Removes a mapping with the specified key from this Map.booleanRemoves the entry for the specified key only if it is currently mapped to the specified value.Replaces the entry for the specified key only if it is currently mapped to some value.booleanReplaces the entry for the specified key only if currently mapped to the specified value.voidreplaceAll(BiFunction<? super K, ? super V, ? extends V> function) Replaces each entry's value with the result of invoking the given function on that entry.intsize()Returns the number of elements in this map.values()Returns a collection of the values contained in this map.Methods inherited from class AbstractMap
equals, hashCode, toString
-
Constructor Details
-
ConcurrentHashMap
public ConcurrentHashMap() -
ConcurrentHashMap
public ConcurrentHashMap(int initialCapacity) -
ConcurrentHashMap
public ConcurrentHashMap(int initialCapacity, float loadFactor) -
ConcurrentHashMap
public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) -
ConcurrentHashMap
-
-
Method Details
-
size
public int size()Description copied from class:AbstractMapReturns the number of elements in this map.
Returns
the number of elements in this map.
-
mappingCount
public long mappingCount() -
isEmpty
public boolean isEmpty()Description copied from class:AbstractMapReturns whether this map is empty.
Returns
-
get
Description copied from class:AbstractMapReturns the value of the mapping with the specified key.
Parameters
key: the key.
Returns
-
containsKey
Description copied from class:AbstractMapReturns whether this map contains the specified key.
Parameters
key: the key to search for.
Returns
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Returns:
trueif this map contains the specified key,falseotherwise.
-
containsValue
Description copied from class:AbstractMapReturns whether this map contains the specified value.
Parameters
value: the value to search for.
Returns
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V> - Returns:
trueif this map contains the specified value,falseotherwise.
-
contains
-
put
Description copied from class:AbstractMapMaps the specified key to the specified value.
Parameters
-
key: the key. -
value: the value.
Returns
- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Returns:
the value of any previous mapping with the specified key or
nullif there was no mapping.Throws
-
UnsupportedOperationException: if adding to this map is not supported. -
ClassCastException: @throws ClassCastException if the class of the key or value is inappropriate for this map. -
IllegalArgumentException: if the key or value cannot be added to this map. -
NullPointerException: @throws NullPointerException if the key or value isnulland this Map does not supportnullkeys or values.
-
-
-
putAll
Description copied from class:AbstractMapCopies every mapping in the specified map to this map.
Parameters
map: the map to copy mappings from.
Throws
-
UnsupportedOperationException: if adding to this map is not supported. -
ClassCastException: @throws ClassCastException if the class of a key or value is inappropriate for this map. -
IllegalArgumentException: if a key or value cannot be added to this map. -
NullPointerException: @throws NullPointerException if a key or value isnulland this map does not supportnullkeys or values.
-
remove
Description copied from class:AbstractMapRemoves a mapping with the specified key from this Map.
Parameters
key: the key of the mapping to remove.
Returns
-
clear
public void clear()Description copied from class:AbstractMapRemoves all elements from this map, leaving it empty.
Throws
UnsupportedOperationException: if removing from this map is not supported.
See also
-
#isEmpty()
-
#size()
-
getOrDefault
-
putIfAbsent
Description copied from interface:MapIf the specified key is not already associated with a value (or is mapped tonull) associates it with the given value and returnsnull, else returns the current value.- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
putIfAbsentin interfaceMap<K,V>
-
remove
-
replace
-
replace
-
computeIfAbsent
Description copied from interface:MapIf the specified key is not already associated with a value (or is mapped tonull), attempts to compute its value using the given mapping function and enters it into this map unlessnull.- Specified by:
computeIfAbsentin interfaceMap<K,V>
-
computeIfPresent
Description copied from interface:MapIf the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.- Specified by:
computeIfPresentin interfaceMap<K,V>
-
compute
-
merge
Description copied from interface:MapIf the specified key is not already associated with a value or is associated withnull, associates it with the given non-null value. Otherwise, replaces the associated value with the result of the given remapping function, or removes if the result isnull. -
forEach
-
replaceAll
Description copied from interface:MapReplaces each entry's value with the result of invoking the given function on that entry.- Specified by:
replaceAllin interfaceMap<K,V>
-
keySet
-
keySet
-
newKeySet
-
newKeySet
-
values
Description copied from class:AbstractMapReturns a collection of the values contained in this map. The collection is backed by this map so changes to one are reflected by the other. The collection supports remove, removeAll, retainAll and clear operations, and it does not support add or addAll operations.
This method returns a collection which is the subclass of AbstractCollection. The iterator method of this subclass returns a "wrapper object" over the iterator of map's entrySet(). The
sizemethod wraps the map's size method and thecontainsmethod wraps the map's containsValue method.The collection is created when this method is called for the first time and returned in response to all subsequent calls. This method may return different collections when multiple concurrent calls occur to this method, since no synchronization is performed.
Returns
a collection of the values contained in this map.
-
entrySet
Description copied from class:AbstractMapReturns a set containing all of the mappings in this map. Each mapping is an instance of
Map.Entry. As the set is backed by this map, changes in one will be reflected in the other.Returns
a set of the mappings.
-
keys
-
elements
-