Class ConcurrentWeakHashMap<K,​V>

  • All Implemented Interfaces:
    java.util.concurrent.ConcurrentMap<K,​V>, java.util.Map<K,​V>

    public class ConcurrentWeakHashMap<K,​V>
    extends java.util.AbstractMap<K,​V>
    implements java.util.concurrent.ConcurrentMap<K,​V>
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ConcurrentWeakHashMap.LazyValueCreator<K,​V>  
      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • 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
      ConcurrentWeakHashMap()
      Constructs a new, empty HashMap with a default capacity and load factor.
      ConcurrentWeakHashMap​(int initialCapacity)
      Constructs a new, empty HashMap with the specified initial capacity and default load factor.
      ConcurrentWeakHashMap​(int initialCapacity, float loadFactor, int concurrencyLevel)
      Constructs a new, empty HashMap with the specified initial capacity and the specified load factor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear all mappings from all segment, leaving the ConcurrentWeakHashMap empty.
      boolean containsKey​(java.lang.Object key)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)
      Returns the value to which this HashMap maps the specified key.
      V getOrPut​(K key, ConcurrentWeakHashMap.LazyValueCreator<K,​V> valueCreator)
      Checks if a value is already mapped and returns that one.
      java.util.Set<K> keySet()  
      V put​(K key, V value)  
      V putIfAbsent​(K key, V value)  
      V remove​(java.lang.Object key)  
      boolean remove​(java.lang.Object key, java.lang.Object value)  
      V replace​(K key, V value)  
      boolean replace​(K key, V oldValue, V newValue)  
      int size()
      This function is not accurate under concurrent environment.
      • Methods inherited from class java.util.AbstractMap

        clone, containsValue, equals, hashCode, isEmpty, putAll, toString, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, 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

        containsValue, equals, hashCode, isEmpty, putAll, values
    • Constructor Detail

      • ConcurrentWeakHashMap

        public ConcurrentWeakHashMap​(int initialCapacity,
                                     float loadFactor,
                                     int concurrencyLevel)
        Constructs a new, empty HashMap with the specified initial capacity and the specified load factor.
        Parameters:
        initialCapacity - the initial capacity of the HashMap.
        loadFactor - a number between 0.0 and 1.0.
        concurrencyLevel - the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
        Throws:
        java.lang.IllegalArgumentException - if neither keys nor values use hard references, if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero
      • ConcurrentWeakHashMap

        public ConcurrentWeakHashMap​(int initialCapacity)
        Constructs a new, empty HashMap with the specified initial capacity and default load factor.
        Parameters:
        initialCapacity - the initial capacity of the HashMap.
      • ConcurrentWeakHashMap

        public ConcurrentWeakHashMap()
        Constructs a new, empty HashMap with a default capacity and load factor.
    • Method Detail

      • put

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

        public V get​(java.lang.Object key)
        Returns the value to which this HashMap maps the specified key. Returns null if the HashMap contains no mapping for this key.
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
        Parameters:
        key - key whose associated value is to be returned.
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.AbstractMap<K,​V>
      • 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>
      • size

        public int size()
        This function is not accurate under concurrent environment.
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.AbstractMap<K,​V>
      • containsKey

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

        public void clear()
        Clear all mappings from all segment, leaving the ConcurrentWeakHashMap empty.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.AbstractMap<K,​V>
      • keySet

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

        public V getOrPut​(K key,
                          ConcurrentWeakHashMap.LazyValueCreator<K,​V> valueCreator)
        Checks if a value is already mapped and returns that one. If it does not find that key a new value is lazily created and returned as result.

        Please note that the returned value is always the one which stays inside the map after that call, which is totally different to traditional methods like put or putIfAbsent!

      • 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>
      • 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>
      • 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>
      • entrySet

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