Class ConcurrentHashMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.concurrent.ConcurrentHashMap<K,V>
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>, Map<K,V>

public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<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.

See Also:
  • 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 int size()
      Description copied from class: AbstractMap

      Returns the number of elements in this map.

      Returns

      the number of elements in this map.

      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • mappingCount

      public long mappingCount()
    • isEmpty

      public boolean isEmpty()
      Description copied from class: AbstractMap

      Returns whether this map is empty.

      Returns
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
      Returns:

      true if this map has no elements, false otherwise.

      See also
      • #size()
    • get

      public V get(Object key)
      Description copied from class: AbstractMap

      Returns the value of the mapping with the specified key.

      Parameters
      • key: the key.
      Returns
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Returns:
      the value of the mapping with the specified key, or null if no mapping for the specified key is found.
    • containsKey

      public boolean containsKey(Object key)
      Description copied from class: AbstractMap

      Returns whether this map contains the specified key.

      Parameters
      • key: the key to search for.
      Returns
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Returns:
      true if this map contains the specified key, false otherwise.
    • containsValue

      public boolean containsValue(Object value)
      Description copied from class: AbstractMap

      Returns whether this map contains the specified value.

      Parameters
      • value: the value to search for.
      Returns
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Returns:
      true if this map contains the specified value, false otherwise.
    • contains

      public boolean contains(Object value)
    • put

      public V put(K key, V value)
      Description copied from class: AbstractMap

      Maps the specified key to the specified value.

      Parameters
      • key: the key.

      • value: the value.

      Returns
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      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: @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 is null and this Map does not support null keys or values.

    • putAll

      public void putAll(Map<? extends K, ? extends V> m)
      Description copied from class: AbstractMap

      Copies 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 is null and this map does not support null keys or values.

      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
    • remove

      public V remove(Object key)
      Description copied from class: AbstractMap

      Removes a mapping with the specified key from this Map.

      Parameters
      • key: the key of the mapping to remove.
      Returns
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      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 void clear()
      Description copied from class: AbstractMap

      Removes all elements from this map, leaving it empty.

      Throws
      • UnsupportedOperationException: if removing from this map is not supported.
      See also
      • #isEmpty()

      • #size()

      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Description copied from interface: Map
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
      Specified by:
      getOrDefault in interface Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Description copied from interface: Map
      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.
      Specified by:
      putIfAbsent in interface ConcurrentMap<K,V>
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Description copied from interface: Map
      Removes the entry for the specified key only if it is currently mapped to the specified value.
      Specified by:
      remove in interface ConcurrentMap<K,V>
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Description copied from interface: Map
      Replaces the entry for the specified key only if currently mapped to the specified value.
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Description copied from interface: Map
      Replaces the entry for the specified key only if it is currently mapped to some value.
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
      Description copied from interface: Map
      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.
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • computeIfPresent

      public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Description copied from interface: Map
      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.
      Specified by:
      computeIfPresent in interface Map<K,V>
    • compute

      public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Description copied from interface: Map
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
      Description copied from interface: Map
      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.
      Specified by:
      merge in interface Map<K,V>
    • forEach

      public void forEach(BiConsumer<? super K, ? super V> action)
      Description copied from interface: Map
      Performs the given action for each entry in this map.
      Specified by:
      forEach in interface Map<K,V>
    • replaceAll

      public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
      Description copied from interface: Map
      Replaces each entry's value with the result of invoking the given function on that entry.
      Specified by:
      replaceAll in interface Map<K,V>
    • 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.
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
    • 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()
      Description copied from class: AbstractMap

      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.

      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Description copied from class: AbstractMap

      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.

      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • keys

      public Enumeration<K> keys()
    • elements

      public Enumeration<V> elements()