Class ConcurrentHashMap<K,V>

All Implemented Interfaces:
Externalizable, Serializable, Cloneable, Iterable<V>, ConcurrentMap<K,V>, Map<K,V>, InternalIterable<V>, ConcurrentMutableMap<K,V>, MapIterable<K,V>, MutableMap<K,V>, MutableMapIterable<K,V>, UnsortedMapIterable<K,V>, RichIterable<V>

public final class ConcurrentHashMap<K,V> extends AbstractMutableMap<K,V> implements ConcurrentMutableMap<K,V>, Externalizable
See Also:
  • Constructor Details

    • ConcurrentHashMap

      public ConcurrentHashMap()
    • ConcurrentHashMap

      public ConcurrentHashMap(int initialCapacity)
  • Method Details

    • newMap

      public static <K, V> ConcurrentHashMap<K,V> newMap()
    • newMap

      public static <K, V> ConcurrentHashMap<K,V> newMap(int newSize)
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface ConcurrentMap<K,V>
      Specified by:
      putIfAbsent in interface Map<K,V>
    • getIfAbsentPut

      public V getIfAbsentPut(K key, Function<? super K,? extends V> factory)
    • getIfAbsentPut

      public V getIfAbsentPut(K key, Function0<? extends V> factory)
      Description copied from interface: MutableMapIterable
      Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map at the key, return the result of evaluating the specified Function0, and put that value in the map at the specified key.
      Specified by:
      getIfAbsentPut in interface MutableMapIterable<K,V>
      Overrides:
      getIfAbsentPut in class AbstractMutableMapIterable<K,V>
    • getIfAbsentPut

      public V getIfAbsentPut(K key, V value)
      Description copied from interface: MutableMapIterable
      Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map at the key, return the specified value, and put that value in the map at the specified key.
      Specified by:
      getIfAbsentPut in interface MutableMapIterable<K,V>
      Overrides:
      getIfAbsentPut in class AbstractMutableMapIterable<K,V>
    • putIfAbsentGetIfPresent

      public <P1, P2> V putIfAbsentGetIfPresent(K key, Function2<? super K,? super V,? extends K> keyTransformer, Function3<P1,P2,? super K,? extends V> factory, P1 param1, P2 param2)
      It puts an object into the map based on the key. It uses a copy of the key converted by transformer.
      Parameters:
      key - The "mutable" key, which has the same identity/hashcode as the inserted key, only during this call
      keyTransformer - If the record is absent, the transformer will transform the "mutable" key into an immutable copy of the key. Note that the transformed key must have the same identity/hashcode as the original "mutable" key.
      factory - It creates an object, if it is not present in the map already.
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface ConcurrentMap<K,V>
      Specified by:
      remove in interface Map<K,V>
    • size

      public int size()
      Description copied from interface: RichIterable
      Returns the number of items in this iterable.
      Specified by:
      size in interface Map<K,V>
      Specified by:
      size in interface RichIterable<K>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RichIterable
      Returns true if this iterable has zero items.
      Specified by:
      isEmpty in interface Map<K,V>
      Specified by:
      isEmpty in interface RichIterable<K>
      Overrides:
      isEmpty in class AbstractRichIterable<V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Specified by:
      containsKey in interface MapIterable<K,V>
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Specified by:
      containsValue in interface MapIterable<K,V>
      See Also:
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Specified by:
      get in interface MapIterable<K,V>
      See Also:
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • putAllInParallel

      public void putAllInParallel(Map<? extends K,? extends V> map, int chunks, Executor executor)
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • parallelForEachKeyValue

      public void parallelForEachKeyValue(List<Procedure2<K,V>> blocks, Executor executor)
    • parallelForEachValue

      public void parallelForEachValue(List<Procedure<V>> blocks, Executor executor)
    • hashCode

      public int hashCode()
      Description copied from interface: MapIterable
      Follows the same general contract as Map.hashCode().
      Specified by:
      hashCode in interface Map<K,V>
      Specified by:
      hashCode in interface MapIterable<K,V>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Description copied from interface: MapIterable
      Follows the same general contract as Map.equals(Object).
      Specified by:
      equals in interface Map<K,V>
      Specified by:
      equals in interface MapIterable<K,V>
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Description copied from class: AbstractRichIterable
      Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.
       Assert.assertEquals("[]", Lists.mutable.empty().toString());
       Assert.assertEquals("[1]", Lists.mutable.with(1).toString());
       Assert.assertEquals("[1, 2, 3]", Lists.mutable.with(1, 2, 3).toString());
       
      Specified by:
      toString in interface MapIterable<K,V>
      Specified by:
      toString in interface RichIterable<K>
      Overrides:
      toString in class AbstractRichIterable<V>
      Returns:
      a string representation of this collection.
      See Also:
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • newMap

      public static <NK, NV> ConcurrentHashMap<NK,NV> newMap(Map<NK,NV> map)
    • withKeyValue

      public ConcurrentHashMap<K,V> withKeyValue(K key, V value)
      Description copied from interface: MutableMapIterable
      This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original plus the additional key and value. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
       map = map.withKeyValue("new key", "new value");
       
      In the case of FixedSizeMap, a new instance will be returned by withKeyValue, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling put on themselves.
      Specified by:
      withKeyValue in interface MutableMap<K,V>
      Specified by:
      withKeyValue in interface MutableMapIterable<K,V>
      Overrides:
      withKeyValue in class AbstractMutableMap<K,V>
      See Also:
    • withMap

      public ConcurrentHashMap<K,V> withMap(Map<? extends K,? extends V> map)
      Description copied from interface: MutableMapIterable
      Similar to Map.putAll(Map), but returns this instead of void
      Specified by:
      withMap in interface ConcurrentMutableMap<K,V>
      Specified by:
      withMap in interface MutableMap<K,V>
      Specified by:
      withMap in interface MutableMapIterable<K,V>
      See Also:
    • withMapIterable

      public ConcurrentHashMap<K,V> withMapIterable(MapIterable<? extends K,? extends V> mapIterable)
      Specified by:
      withMapIterable in interface ConcurrentMutableMap<K,V>
      Specified by:
      withMapIterable in interface MutableMap<K,V>
      Specified by:
      withMapIterable in interface MutableMapIterable<K,V>
    • withAllKeyValues

      public ConcurrentHashMap<K,V> withAllKeyValues(Iterable<? extends Pair<? extends K,? extends V>> keyValues)
      Description copied from interface: MutableMapIterable
      This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original plus all the additional keys and values. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
       map = map.withAllKeyValues(FastList.newListWith(PairImpl.of("new key", "new value")));
       
      In the case of FixedSizeMap, a new instance will be returned by withAllKeyValues, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling put on themselves.
      Specified by:
      withAllKeyValues in interface MutableMap<K,V>
      Specified by:
      withAllKeyValues in interface MutableMapIterable<K,V>
      Overrides:
      withAllKeyValues in class AbstractMutableMap<K,V>
      See Also:
    • withAllKeyValueArguments

      public ConcurrentHashMap<K,V> withAllKeyValueArguments(Pair<? extends K,? extends V>... keyValues)
      Description copied from interface: MutableMapIterable
      Convenience var-args version of withAllKeyValues
      Specified by:
      withAllKeyValueArguments in interface MutableMap<K,V>
      Specified by:
      withAllKeyValueArguments in interface MutableMapIterable<K,V>
      Overrides:
      withAllKeyValueArguments in class AbstractMutableMap<K,V>
      See Also:
    • withoutKey

      public ConcurrentHashMap<K,V> withoutKey(K key)
      Description copied from interface: MutableMapIterable
      This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original minus the key and value to be removed. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
       map = map.withoutKey("key");
       
      In the case of FixedSizeMap, a new instance will be returned by withoutKey, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling remove on themselves.
      Specified by:
      withoutKey in interface MutableMap<K,V>
      Specified by:
      withoutKey in interface MutableMapIterable<K,V>
      Overrides:
      withoutKey in class AbstractMutableMap<K,V>
      See Also:
    • withoutAllKeys

      public ConcurrentHashMap<K,V> withoutAllKeys(Iterable<? extends K> keys)
      Description copied from interface: MutableMapIterable
      This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original minus all the keys and values to be removed. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
       map = map.withoutAllKeys(FastList.newListWith("key1", "key2"));
       
      In the case of FixedSizeMap, a new instance will be returned by withoutAllKeys, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling remove on themselves.
      Specified by:
      withoutAllKeys in interface MutableMap<K,V>
      Specified by:
      withoutAllKeys in interface MutableMapIterable<K,V>
      Overrides:
      withoutAllKeys in class AbstractMutableMap<K,V>
      See Also:
    • clone

      public MutableMap<K,V> clone()
      Specified by:
      clone in interface MutableMap<K,V>
      Specified by:
      clone in class AbstractMutableMap<K,V>
    • newEmpty

      public <K, V> MutableMap<K,V> newEmpty(int capacity)
      Description copied from class: AbstractMutableMap
      Creates a new instance of the same type, using the given capacity and the default growth parameters.
      Specified by:
      newEmpty in class AbstractMutableMap<K,V>
    • notEmpty

      public boolean notEmpty()
      Description copied from interface: RichIterable
      The English equivalent of !this.isEmpty()
      Specified by:
      notEmpty in interface RichIterable<K>
    • forEachWithIndex

      public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
      Description copied from interface: InternalIterable
      Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.

      Example using a Java 8 lambda:

       people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
       

      Example using an anonymous inner class:

       people.forEachWithIndex(new ObjectIntProcedure<Person>()
       {
           public void value(Person person, int index)
           {
               LOGGER.info("Index: " + index + " person: " + person.getName());
           }
       });
       
      Specified by:
      forEachWithIndex in interface InternalIterable<K>
      Overrides:
      forEachWithIndex in class AbstractMapIterable<K,V>
    • iterator

      public Iterator<V> iterator()
      Specified by:
      iterator in interface Iterable<K>
      Overrides:
      iterator in class AbstractMutableMapIterable<K,V>
    • newEmpty

      public MutableMap<K,V> newEmpty()
      Description copied from interface: MutableMapIterable
      Creates a new instance of the same type, using the default capacity and growth parameters.
      Specified by:
      newEmpty in interface MutableMap<K,V>
      Specified by:
      newEmpty in interface MutableMapIterable<K,V>
    • tap

      public ConcurrentMutableMap<K,V> tap(Procedure<? super V> procedure)
      Description copied from interface: MapIterable
      Executes the Procedure for each value of the map and returns this.
       return peopleByCity.tap(person -> LOGGER.info(person.getName()));
       
      Specified by:
      tap in interface ConcurrentMutableMap<K,V>
      Specified by:
      tap in interface MapIterable<K,V>
      Specified by:
      tap in interface MutableMap<K,V>
      Specified by:
      tap in interface MutableMapIterable<K,V>
      Specified by:
      tap in interface RichIterable<K>
      Specified by:
      tap in interface UnsortedMapIterable<K,V>
      Overrides:
      tap in class AbstractMutableMap<K,V>
      See Also:
    • forEachValue

      public void forEachValue(Procedure<? super V> procedure)
      Description copied from interface: MapIterable
      Calls the procedure with each value of the map.
           Set<String> result = UnifiedSet.newSet();
           MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
           map.forEachValue(new CollectionAddProcedure<String>(result));
           Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
       
      Specified by:
      forEachValue in interface MapIterable<K,V>
      Overrides:
      forEachValue in class AbstractMapIterable<K,V>
    • forEachKey

      public void forEachKey(Procedure<? super K> procedure)
      Description copied from interface: MapIterable
      Calls the procedure with each key of the map.
           final Collection<Integer> result = new ArrayList<Integer>();
           MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
           map.forEachKey(new CollectionAddProcedure<Integer>(result));
           Verify.assertContainsAll(result, 1, 2, 3);
       
      Specified by:
      forEachKey in interface MapIterable<K,V>
      Overrides:
      forEachKey in class AbstractMapIterable<K,V>
    • forEachKeyValue

      public void forEachKeyValue(Procedure2<? super K,? super V> procedure)
      Description copied from interface: MapIterable
      Calls the procedure with each key-value pair of the map.
           final Collection<String> collection = new ArrayList<String>();
           MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
           map.forEachKeyValue((Integer key, String value) -> collection.add(String.valueOf(key) + value));
           Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
       
      Specified by:
      forEachKeyValue in interface MapIterable<K,V>
    • collectKeysAndValues

      public <E> MutableMap<K,V> collectKeysAndValues(Iterable<E> iterable, Function<? super E,? extends K> keyFunction, Function<? super E,? extends V> valueFunction)
      Description copied from interface: MutableMap
      Adds all the entries derived from iterable to this. The key and value for each entry is determined by applying the keyFunction and valueFunction to each item in collection. Any entry in map that has the same key as an entry in this will have its value replaced by that in map.
      Specified by:
      collectKeysAndValues in interface MutableMap<K,V>
    • removeKey

      public V removeKey(K key)
      Description copied from interface: MutableMapIterable
      Remove an entry from the map at the specified key.
      Specified by:
      removeKey in interface MutableMapIterable<K,V>
      Returns:
      The value removed from entry at key, or null if not found.
      See Also:
    • getIfAbsentPutWith

      public <P> V getIfAbsentPutWith(K key, Function<? super P,? extends V> function, P parameter)
      Description copied from interface: MutableMapIterable
      Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map for that key return the result of evaluating the specified Function using the specified parameter, and put that value in the map at the specified key.
      Specified by:
      getIfAbsentPutWith in interface MutableMapIterable<K,V>
      Overrides:
      getIfAbsentPutWith in class AbstractMutableMapIterable<K,V>
    • getIfAbsent

      public V getIfAbsent(K key, Function0<? extends V> function)
      Description copied from interface: MapIterable
      Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the result of evaluating the specified Function0.
      Specified by:
      getIfAbsent in interface MapIterable<K,V>
      Overrides:
      getIfAbsent in class AbstractMapIterable<K,V>
    • getIfAbsentWith

      public <P> V getIfAbsentWith(K key, Function<? super P,? extends V> function, P parameter)
      Description copied from interface: MapIterable
      Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the result of evaluating the specified function and parameter.
      Specified by:
      getIfAbsentWith in interface MapIterable<K,V>
      Overrides:
      getIfAbsentWith in class AbstractMapIterable<K,V>
    • ifPresentApply

      public <A> A ifPresentApply(K key, Function<? super V,? extends A> function)
      Description copied from interface: MapIterable
      If there is a value in the Map that corresponds to the specified key return the result of applying the specified Function on the value, otherwise return null.
      Specified by:
      ifPresentApply in interface MapIterable<K,V>
      Overrides:
      ifPresentApply in class AbstractMapIterable<K,V>
    • forEachWith

      public <P> void forEachWith(Procedure2<? super V,? super P> procedure, P parameter)
      Description copied from interface: InternalIterable
      The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.

      Example using a Java 8 lambda:

       people.forEachWith((Person person, Person other) ->
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }, fred);
       

      Example using an anonymous inner class:

       people.forEachWith(new Procedure2<Person, Person>()
       {
           public void value(Person person, Person other)
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }
       }, fred);
       
      Specified by:
      forEachWith in interface InternalIterable<K>
      Overrides:
      forEachWith in class AbstractMapIterable<K,V>
    • updateValue

      public V updateValue(K key, Function0<? extends V> factory, Function<? super V,? extends V> function)
      Description copied from interface: MutableMapIterable
      Looks up the value associated with key, applies the function to it, and replaces the value. If there is no value associated with key, starts it off with a value supplied by factory.
      Specified by:
      updateValue in interface MutableMapIterable<K,V>
      Overrides:
      updateValue in class AbstractMutableMapIterable<K,V>
    • updateValueWith

      public <P> V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V,? super P,? extends V> function, P parameter)
      Description copied from interface: MutableMapIterable
      Same as MutableMapIterable.updateValue(Object, Function0, Function) with a Function2 and specified parameter which is passed to the function.
      Specified by:
      updateValueWith in interface MutableMapIterable<K,V>
      Overrides:
      updateValueWith in class AbstractMutableMapIterable<K,V>
    • toImmutable

      public ImmutableMap<K,V> toImmutable()
      Description copied from interface: MutableMapIterable
      Returns an immutable copy of this map. If the map is immutable, it returns itself.
      Specified by:
      toImmutable in interface MapIterable<K,V>
      Specified by:
      toImmutable in interface MutableMapIterable<K,V>
      Specified by:
      toImmutable in interface UnsortedMapIterable<K,V>
      Overrides:
      toImmutable in class AbstractMutableMap<K,V>