Class ConcurrentIdentityHashMap<K,​V>

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

    @ThreadSafe
    public class ConcurrentIdentityHashMap<K,​V>
    extends java.lang.Object
    implements java.util.concurrent.ConcurrentMap<K,​V>
    An implementation ConcurrentHashMap that utilizes identity semantics with its keys Having identity semantics means that it is possible to have two objects with which are equal based on the object's equals() implementation, but not be overridden in the hash map if you try to put both. For example:
       ConcurrentIdentityHashMap<String, Boolean> map = new ConcurrentIdentityHashMap<>();
       String s1 = new String("test");
       String s2 = new String("test");
       map.put(s1, true);
       map.put(s2, false);
       map.size(); // Prints "2".
       map.get(s1); // true
       map.get(s2); // false
     
    Keys in this mapped are hashed based on the identity, that is, the location in memory of the objects rather than the equality which java typically uses for comparison. This implementation of ConcurrentMap has a few limitations which the corresponding ConcurrentHashMap does not have. - This map's entrySet() returns a copy of the entries at the time of calling the method. The returned set does will not receive updates from the underlying map. This is a departure from the behavior of ConcurrentHashMap - This map's keySet() does return the map-backed keySet and provides the same behavior as in ConcurrentHashMap, however the difference is that Set.toArray() and Set.toArray(Object[]) methods are left unimplemented and will throw an error if the user attempts to convert the set into an array.
    • 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>
    • Constructor Summary

      Constructors 
      Constructor Description
      ConcurrentIdentityHashMap()
      Creates a new, empty map with the default initial table size (16).
      ConcurrentIdentityHashMap​(int initialCapacity)
      Creates a new, empty map with an initial table size accommodating the specified number of elements without having to dynamically resize.
      ConcurrentIdentityHashMap​(int initialCapacity, float loadFactor)
      Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity) and initial load factor (loadFactor) with a concurrencyLevel of 1.
      ConcurrentIdentityHashMap​(int initialCapacity, float loadFactor, int concurrencyLevel)
      Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity), load factor (loadFactor), and number of expected concurrently updating threads (concurrencyLevel).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object k)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a set representing the entries of this map at the time of calling.
      V get​(java.lang.Object k)  
      boolean isEmpty()  
      java.util.Set<K> keySet()
      Returns a set representing all keys contained within the map.
      V put​(K k, V v)  
      void putAll​(java.util.Map<? extends K,​? extends V> map)  
      V putIfAbsent​(K k, V v)  
      V remove​(java.lang.Object k)  
      boolean remove​(java.lang.Object k, java.lang.Object v)  
      V replace​(K k, V v)  
      boolean replace​(K k, V v1, V v2)  
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.concurrent.ConcurrentMap

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
      • Methods inherited from interface java.util.Map

        equals, hashCode
    • Constructor Detail

      • ConcurrentIdentityHashMap

        public ConcurrentIdentityHashMap()
        Creates a new, empty map with the default initial table size (16).
      • ConcurrentIdentityHashMap

        public ConcurrentIdentityHashMap​(int initialCapacity)
        Creates a new, empty map with an initial table size accommodating the specified number of elements without having to dynamically resize.
        Parameters:
        initialCapacity - The implementation performs internal sizing to accommodate this many elements.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity of elements is negative
      • ConcurrentIdentityHashMap

        public ConcurrentIdentityHashMap​(int initialCapacity,
                                         float loadFactor)
        Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity) and initial load factor (loadFactor) with a concurrencyLevel of 1.
        Parameters:
        initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements, given the specified load factor.
        loadFactor - the load factor (table density) for establishing the initial table size
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity of elements is negative or the load factor is nonpositive
      • ConcurrentIdentityHashMap

        public ConcurrentIdentityHashMap​(int initialCapacity,
                                         float loadFactor,
                                         int concurrencyLevel)
        Creates a new, empty map with an initial table size based on the given number of elements (initialCapacity), load factor (loadFactor), and number of expected concurrently updating threads (concurrencyLevel).
        Parameters:
        initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements, given the specified load factor.
        loadFactor - the load factor (table density) for establishing the initial table size
        concurrencyLevel - the estimated number of concurrently updating threads. The implementation may use this value as a sizing hint.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor or concurrencyLevel are nonpositive
    • Method Detail

      • containsKey

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

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

        public V putIfAbsent​(K k,
                             V v)
        Specified by:
        putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
      • remove

        public boolean remove​(java.lang.Object k,
                              java.lang.Object v)
        Specified by:
        remove in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
      • remove

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

        public boolean replace​(K k,
                               V v1,
                               V v2)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • replace

        public V replace​(K k,
                         V v)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • get

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

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

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

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a set representing the entries of this map at the time of calling. Note, this method will create a copy of the map's entry set at creation time. Standard behavior in the ConcurrentHashMap says that the entry set backed by the map is updated when the underlying map is updated. That is not the case for this class.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        The set of entries in the map at time of calling
      • putAll

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

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

        public int size()
        Specified by:
        size 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>
      • keySet

        public java.util.Set<K> keySet()
        Returns a set representing all keys contained within the map. Note this method departs slightly from the standard ConcurrentHashMap implementation by providing its own implementation of a Set for ConcurrentIdentityHashMap.IdentityObject. Not all methods have been implemented. Namely the toArray, add*, and *all(Collection<?> c) methods.
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        The set of keys in the map