Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class ConcurrentHashMap<K, V>

  1. Object
  2. AbstractMap<K, V>
  3. ConcurrentHashMap

ImplementsConcurrentMap<K, V>, Map<K, V>, Serializable

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:

  • null keys and values are rejected with NullPointerException.
  • 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.remove and Map.Entry.setValue write through to the map.

The bulk parallel operations (forEach(long, ...), search, reduce) and forEachKey/forEachValue are not provided.

Nested types

class ConcurrentHashMap.KeySetViewjava.util.concurrent.ConcurrentHashMap.KeySetView: the keys of a map as a set.

Constructors

public ConcurrentHashMap()
public ConcurrentHashMap(int initialCapacity)
public ConcurrentHashMap(int initialCapacity, float loadFactor)
public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
public ConcurrentHashMap(Map<? extends K, ? extends V> m)

Methods

public synchronized int size()Returns the number of elements in this map.
public synchronized long mappingCount()
public synchronized boolean isEmpty()Returns whether this map is empty.
public synchronized V get(Object key)Returns the value of the mapping with the specified key.
public synchronized boolean containsKey(Object key)Returns whether this map contains the specified key.
public synchronized boolean containsValue(Object value)Returns whether this map contains the specified value.
public boolean contains(Object value)
public synchronized V put(K key, V value)Maps the specified key to the specified value.
public void putAll(Map<? extends K, ? extends V> m)Copies every mapping in the specified map to this map.
public synchronized V remove(Object key)Removes a mapping with the specified key from this Map.
public synchronized void clear()Removes all elements from this map, leaving it empty.
public V getOrDefault(Object key, V defaultValue)Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
public synchronized V putIfAbsent(K key, V value)If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
public synchronized boolean remove(Object key, Object value)Removes the entry for the specified key only if it is currently mapped to the specified value.
public synchronized boolean replace(K key, V oldValue, V newValue)Replaces the entry for the specified key only if currently mapped to the specified value.
public synchronized V replace(K key, V value)Replaces the entry for the specified key only if it is currently mapped to some value.
public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
public synchronized V 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.
public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
public void forEach(BiConsumer<? super K, ? super V> action)Performs the given action for each entry in this map.
public synchronized void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)Replaces each entry’s value with the result of invoking the given function on that entry.
public ConcurrentHashMap.KeySetView<K, V> keySet()The keys, as a view of this map.
public ConcurrentHashMap.KeySetView<K, V> keySet(V mappedValue)
public static <K> ConcurrentHashMap.KeySetView<K, Boolean> newKeySet()
public static <K> ConcurrentHashMap.KeySetView<K, Boolean> newKeySet(int initialCapacity)
public Collection<V> values()Returns a collection of the values contained in this map.
public Set<Map.Entry<K, V>> entrySet()Returns a set containing all of the mappings in this map.
public Enumeration<K> keys()
public Enumeration<V> elements()

Inherited nested types

Inherited methods

From Map

of, of, of, of, of, of, of, of, of, of, of, ofEntries, entry, copyOf

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

public ConcurrentHashMap(Map<? extends K, ? extends V> m)

Method details

size

public synchronized int size()
Returns the number of elements in this map.

Returns

the number of elements in this map.

mappingCount

public synchronized long mappingCount()

isEmpty

public synchronized boolean isEmpty()
Returns whether this map is empty.

Returns

true if this map has no elements, false otherwise.

See also

get

public synchronized V get(Object key)
Returns the value of the mapping with the specified key.

Parameters

key Object
the key.

Returns

the value of the mapping with the specified key, or null if no mapping for the specified key is found.

containsKey

public synchronized boolean containsKey(Object key)
Returns whether this map contains the specified key.

Parameters

key Object
the key to search for.

Returns

true if this map contains the specified key, false otherwise.

containsValue

public synchronized boolean containsValue(Object value)
Returns whether this map contains the specified value.

Parameters

value Object
the value to search for.

Returns

true if this map contains the specified value, false otherwise.

contains

public boolean contains(Object value)

put

public synchronized V put(K key, V value)
Maps the specified key to the specified value.

Parameters

key K
the key.
value V
the value.

Returns

the value of any previous mapping with the specified key or null if there was no mapping.

Throws

UnsupportedOperationException
if adding to this map is not supported.
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
if the key or value is null and this Map does not support null keys or values.

putAll

public void putAll(Map<? extends K, ? extends V> m)
Copies every mapping in the specified map to this map.

Parameters

m Map<? extends K, ? extends V>
the map to copy mappings from.

Throws

UnsupportedOperationException
if adding to this map is not supported.
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
if a key or value is null and this map does not support null keys or values.

remove

public synchronized V remove(Object key)
Removes a mapping with the specified key from this Map.

Parameters

key Object
the key of the mapping to remove.

Returns

the value of the removed mapping or null if no mapping for the specified key was found.

Throws

UnsupportedOperationException
if removing from this map is not supported.

clear

public synchronized void clear()
Removes all elements from this map, leaving it empty.

Throws

UnsupportedOperationException
if removing from this map is not supported.

getOrDefault

public V getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

putIfAbsent

public synchronized V putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

remove

public synchronized boolean remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.

replace

public synchronized boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.

replace

public synchronized V replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.

computeIfAbsent

public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.

computeIfPresent

public synchronized V 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.

compute

public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

merge

public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, 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 is null.

forEach

public void forEach(BiConsumer<? super K, ? super V> action)
Performs the given action for each entry in this map.

replaceAll

public synchronized void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
Replaces each entry’s value with the result of invoking the given function on that entry.

keySet

public ConcurrentHashMap.KeySetView<K, V> keySet()
The keys, as a view of this map. Compiled code names the JDK’s return type, ConcurrentHashMap.KeySetView, so that is what this returns.

Returns

a set of the keys.

keySet

public ConcurrentHashMap.KeySetView<K, V> keySet(V mappedValue)

newKeySet

public static <K> ConcurrentHashMap.KeySetView<K, Boolean> newKeySet()

newKeySet

public static <K> ConcurrentHashMap.KeySetView<K, Boolean> newKeySet(int initialCapacity)

values

public Collection<V> values()

Returns 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 size method wraps the map’s size method and the contains method 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

public Set<Map.Entry<K, V>> entrySet()
Returns 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

public Enumeration<K> keys()

elements

public Enumeration<V> elements()