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>
- Object
- AbstractMap<K, V>
- 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:
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.
Nested types
class ConcurrentHashMap.KeySetView | java.util.concurrent.ConcurrentHashMap.KeySetView: the keys of a map as a set. |
Constructors
Methods
Inherited nested types
Inherited methods
From AbstractMap
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
mappingCount
public synchronized long mappingCount()isEmpty
public synchronized boolean isEmpty()Returns
true if this map has no elements, false
otherwise.See also
get
public synchronized V get(Object key)Parameters
keyObject- the key.
Returns
null
if no mapping for the specified key is found.containsKey
public synchronized boolean containsKey(Object key)Parameters
keyObject- the key to search for.
Returns
true if this map contains the specified key,
false otherwise.containsValue
public synchronized boolean containsValue(Object value)Parameters
valueObject- 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)Parameters
keyK- the key.
valueV- the value.
Returns
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
nulland this Map does not supportnullkeys or values.
putAll
public void putAll(Map<? extends K, ? extends V> m)Parameters
mMap<? 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
nulland this map does not supportnullkeys or values.
remove
public synchronized V remove(Object key)Parameters
keyObject- the key of the mapping to remove.
Returns
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()Throws
UnsupportedOperationException- if removing from this map is not supported.
See also
getOrDefault
public V getOrDefault(Object key, V defaultValue)defaultValue if this map contains no mapping for the key.putIfAbsent
public synchronized V putIfAbsent(K key, V value)null) associates it with the given value and returns
null, else returns the current value.remove
public synchronized boolean remove(Object key, Object value)replace
public synchronized boolean replace(K key, V oldValue, V newValue)replace
public synchronized V replace(K key, V value)computeIfAbsent
public synchronized V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)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)compute
public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)null if there is no current mapping).merge
public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)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)replaceAll
public synchronized void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)keySet
public ConcurrentHashMap.KeySetView<K, V> keySet()ConcurrentHashMap.KeySetView, so that is what this returns.Returns
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
entrySet
public Set<Map.Entry<K, V>> entrySet()Map.Entry. As the set is backed by this map,
changes in one will be reflected in the other.Returns
keys
public Enumeration<K> keys()elements
public Enumeration<V> elements()