Class AtomicHashMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map
    V - the type of mapped values
    All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>

    public final class AtomicHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>, java.io.Serializable
    A thread-safe implementation of the Map interface providing advanced concurrency features.

    This implementation is thread-safe, atomic and non-blocking for all of its methods, including both reads and writes. This includes multi-element methods such as putAll(Map) and getAll(Object...) (the latter not a part of the Map interface), as well as all other methods for retrieving, adding, modifying or removing mappings, iteration, etc.

    The map can therefore never be read in a partially modified state, and its exact snapshot state for an arbitrary number of mappings can be obtained at any time.

    This is achieved by internally implementing an immutable variation of a CTRIE (Concurrent Hash-Trie). This structure is composed of a tree of compact (bitmap-managed) arrays that map keys to positions in each of the tree levels depending on the value of a range of bits of its (modified) hash code.

    Key hash codes (32-bit ints) are divided into five 6-bit segments plus one final 2-bit segment. Each of these segments is used, at each level of depth, to compute the position assigned to the key in the compact array (node) living at that level of depth in the ctrie structure. These are compact arrays with a maximum of 64 positions (bitmaps are long values), each of which can contain either a data entry or a link to another node at level + 1. A maximum of 6 levels can exist (0 to 5), and hash collisions only need to be managed at the deepest level. All structures are kept immutable, so modifications in an array (node) at a specific level mean the creation of new nodes from that point up to the root of the tree, and the replacement of the old root with the new one using an atomic compare-and-swap operation.

    Note that, given this implementation is based on immutable tree structures, modifications typically need a higher use of memory than other common implementations of the Map interface.

    New instances of this class can be created by either calling its constructor AtomicHashMap() or by calling any of its static convenience factory AtomicHashMap.of(...) methods: of(), of(k1, v1), of(k1, v1, k2, v2), of(k1, v1, k2, v2, k3, v3), etc.

    Note that this implementation does not keep the insertion order. Iteration order is not guaranteed to be consistent.

    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.atomic.AtomicReference<Root> root  
      private static long serialVersionUID  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      V compute​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)  
      V computeIfAbsent​(K key, java.util.function.Function<? super K,​? extends V> mappingFunction)  
      V computeIfPresent​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      private static Entry entry​(int hash, java.lang.Object key, java.lang.Object value)  
      private static Entry entry​(java.lang.Object key, java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object other)  
      void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)  
      V get​(java.lang.Object key)  
      java.util.Map<K,​V> getAll​(java.lang.Object... keys)  
      V getOrDefault​(java.lang.Object key, V defaultValue)  
      int hashCode()  
      (package private) Root innerRoot()  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V merge​(K key, V newValue, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of()  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)  
      static <K,​V>
      AtomicHashMap<K,​V>
      of​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)  
      V put​(K key, V newValue)  
      void putAll​(java.util.Map<? extends K,​? extends V> newMappings)  
      V putIfAbsent​(K key, V newValue)  
      V remove​(java.lang.Object key)  
      boolean remove​(java.lang.Object key, java.lang.Object oldValue)  
      V replace​(K key, V newValue)  
      boolean replace​(K key, V oldValue, V newValue)  
      void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)  
      int size()  
      AtomicHashStore<K,​V> store()  
      java.lang.String toString()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • root

        private final java.util.concurrent.atomic.AtomicReference<Root> root
    • Constructor Detail

      • AtomicHashMap

        public AtomicHashMap()
      • AtomicHashMap

        public AtomicHashMap​(java.util.Map<? extends K,​? extends V> map)
      • AtomicHashMap

        private AtomicHashMap​(Root root)
    • Method Detail

      • entry

        private static Entry entry​(java.lang.Object key,
                                   java.lang.Object value)
      • entry

        private static Entry entry​(int hash,
                                   java.lang.Object key,
                                   java.lang.Object value)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5,
                                                              K k6,
                                                              V v6)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5,
                                                              K k6,
                                                              V v6,
                                                              K k7,
                                                              V v7)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5,
                                                              K k6,
                                                              V v6,
                                                              K k7,
                                                              V v7,
                                                              K k8,
                                                              V v8)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5,
                                                              K k6,
                                                              V v6,
                                                              K k7,
                                                              V v7,
                                                              K k8,
                                                              V v8,
                                                              K k9,
                                                              V v9)
      • of

        public static <K,​V> AtomicHashMap<K,​V> of​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4,
                                                              K k5,
                                                              V v5,
                                                              K k6,
                                                              V v6,
                                                              K k7,
                                                              V v7,
                                                              K k8,
                                                              V v8,
                                                              K k9,
                                                              V v9,
                                                              K k10,
                                                              V v10)
      • innerRoot

        Root innerRoot()
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsKey

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

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

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

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

        public java.util.Map<K,​V> getAll​(java.lang.Object... keys)
      • put

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

        public V putIfAbsent​(K key,
                             V newValue)
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
      • putAll

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

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

        public boolean remove​(java.lang.Object key,
                              java.lang.Object oldValue)
        Specified by:
        remove 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>
      • forEach

        public void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)
        Specified by:
        forEach in interface java.util.Map<K,​V>
      • replace

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

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

        public void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
        Specified by:
        replaceAll in interface java.util.Map<K,​V>
      • compute

        public V compute​(K key,
                         java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        compute in interface java.util.Map<K,​V>
      • computeIfAbsent

        public V computeIfAbsent​(K key,
                                 java.util.function.Function<? super K,​? extends V> mappingFunction)
        Specified by:
        computeIfAbsent in interface java.util.Map<K,​V>
      • computeIfPresent

        public V computeIfPresent​(K key,
                                  java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        computeIfPresent in interface java.util.Map<K,​V>
      • merge

        public V merge​(K key,
                       V newValue,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        Specified by:
        merge in interface java.util.Map<K,​V>
      • equals

        public boolean equals​(java.lang.Object other)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object