Class ConcurrentHashMap<K,​V>

    • Field Detail

      • RESIZE_SENTINEL

        private static final java.lang.Object RESIZE_SENTINEL
      • DEFAULT_INITIAL_CAPACITY

        private static final int DEFAULT_INITIAL_CAPACITY
        See Also:
        Constant Field Values
      • MAXIMUM_CAPACITY

        private static final int MAXIMUM_CAPACITY
        The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<30.
        See Also:
        Constant Field Values
      • TABLE_UPDATER

        private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentHashMap,​java.util.concurrent.atomic.AtomicReferenceArray> TABLE_UPDATER
      • SIZE_UPDATER

        private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ConcurrentHashMap> SIZE_UPDATER
      • RESIZED

        private static final java.lang.Object RESIZED
      • RESIZING

        private static final java.lang.Object RESIZING
      • PARTITIONED_SIZE_THRESHOLD

        private static final int PARTITIONED_SIZE_THRESHOLD
        See Also:
        Constant Field Values
      • table

        private volatile java.util.concurrent.atomic.AtomicReferenceArray table
        The table, resized as necessary. Length MUST Always be a power of two.
      • partitionedSize

        private java.util.concurrent.atomic.AtomicIntegerArray partitionedSize
      • size

        private volatile int size
    • Constructor Detail

      • ConcurrentHashMap

        public ConcurrentHashMap()
      • ConcurrentHashMap

        public ConcurrentHashMap​(int initialCapacity)
    • Method Detail

      • indexFor

        private static int indexFor​(int h,
                                    int length)
      • putIfAbsent

        public V putIfAbsent​(K key,
                             V value)
        Specified by:
        putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
      • incrementSizeAndPossiblyResize

        private void incrementSizeAndPossiblyResize​(java.util.concurrent.atomic.AtomicReferenceArray currentArray,
                                                    int length,
                                                    java.lang.Object prev)
      • hash

        private int hash​(java.lang.Object key)
      • helpWithResizeWhileCurrentIndex

        private java.util.concurrent.atomic.AtomicReferenceArray helpWithResizeWhileCurrentIndex​(java.util.concurrent.atomic.AtomicReferenceArray currentArray,
                                                                                                 int index)
      • helpWithResize

        private java.util.concurrent.atomic.AtomicReferenceArray helpWithResize​(java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • resize

        private void resize​(java.util.concurrent.atomic.AtomicReferenceArray oldTable)
      • resize

        private void resize​(java.util.concurrent.atomic.AtomicReferenceArray oldTable,
                            int newSize)
      • unconditionalCopy

        private void unconditionalCopy​(java.util.concurrent.atomic.AtomicReferenceArray dest,
                                       ConcurrentHashMap.Entry<K,​V> toCopyEntry)
      • getIfAbsentPut

        public V getIfAbsentPut​(K key,
                                Function<? super K,​? extends V> factory)
      • 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​(java.lang.Object key,
                              java.lang.Object value)
        Specified by:
        remove in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
      • addToSize

        private void addToSize​(int value)
      • incrementPartitionedSize

        private boolean incrementPartitionedSize​(int value)
      • incrementLocalSize

        private void incrementLocalSize​(int value)
      • size

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

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

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Specified by:
        containsValue in interface MapIterable<K,​V>
        See Also:
        Map.containsValue(Object)
      • nullSafeEquals

        private boolean nullSafeEquals​(java.lang.Object v,
                                       java.lang.Object value)
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Specified by:
        get in interface MapIterable<K,​V>
        See Also:
        Map.get(Object)
      • slowGet

        private V slowGet​(java.lang.Object key,
                          int hash,
                          int index,
                          java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • slowPut

        private V slowPut​(K key,
                          V value,
                          int hash,
                          java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • putAllInParallel

        public void putAllInParallel​(java.util.Map<? extends K,​? extends V> map,
                                     int chunks,
                                     java.util.concurrent.Executor executor)
      • sequentialPutAll

        private void sequentialPutAll​(java.util.concurrent.atomic.AtomicReferenceArray currentArray,
                                      int start,
                                      int end)
      • putAll

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

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

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

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

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

        public boolean replace​(K key,
                               V oldValue,
                               V newValue)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • slowReplace

        private boolean slowReplace​(K key,
                                    V oldValue,
                                    V newValue,
                                    int hash,
                                    java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • replace

        public V replace​(K key,
                         V value)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • slowReplace

        private V slowReplace​(K key,
                              V value,
                              int hash,
                              java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • slowRemove

        private V slowRemove​(java.lang.Object key,
                             int hash,
                             java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • parallelForEachKeyValue

        public void parallelForEachKeyValue​(java.util.List<Procedure2<K,​V>> blocks,
                                            java.util.concurrent.Executor executor)
      • sequentialForEachKeyValue

        private void sequentialForEachKeyValue​(Procedure2<? super K,​? super V> block,
                                               java.util.concurrent.atomic.AtomicReferenceArray currentArray,
                                               int start,
                                               int end)
      • parallelForEachValue

        public void parallelForEachValue​(java.util.List<Procedure<V>> blocks,
                                         java.util.concurrent.Executor executor)
      • sequentialForEachValue

        private void sequentialForEachValue​(Procedure<V> block,
                                            java.util.concurrent.atomic.AtomicReferenceArray currentArray,
                                            int start,
                                            int end)
      • hashCode

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

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

        public java.lang.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:
        AbstractCollection.toString()
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeExternal

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

        public static <NK,​NV> ConcurrentHashMap<NK,​NV> newMap​(java.util.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:
        Map.put(Object, Object)
      • withAllKeyValues

        public ConcurrentHashMap<K,​V> withAllKeyValues​(java.lang.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:
        Map.put(Object, Object)
      • 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:
        Map.remove(Object)
      • withoutAllKeys

        public ConcurrentHashMap<K,​V> withoutAllKeys​(java.lang.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:
        Map.remove(Object)
      • 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>
      • 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​(java.lang.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:
        Map.remove(Object)
      • 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>
      • slowUpdateValue

        private V slowUpdateValue​(K key,
                                  Function0<? extends V> factory,
                                  Function<? super V,​? extends V> function,
                                  int hash,
                                  java.util.concurrent.atomic.AtomicReferenceArray currentArray)
      • slowUpdateValueWith

        private <P> V slowUpdateValueWith​(K key,
                                          Function0<? extends V> factory,
                                          Function2<? super V,​? super P,​? extends V> function,
                                          P parameter,
                                          int hash,
                                          java.util.concurrent.atomic.AtomicReferenceArray currentArray)