Class WeakHashMap2<K,​V>

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

    public class WeakHashMap2<K,​V>
    extends java.util.AbstractMap<K,​V>
    A hashtable-based Map implementation with weak values.

    This implementation uses two maps internally, which nearly doubles the memory requirements over a traditional map.

    • Nested Class Summary

      • 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>
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakHashMap2()
      Constructs a new, empty WeakHashMap2 with the default initial capacity and the default load factor, which is 0.75.
      WeakHashMap2​(int initialCapacity)
      Constructs a new, empty WeakHashMap2 with the given initial capacity and the default load factor, which is 0.75.
      WeakHashMap2​(int initialCapacity, float loadFactor)
      Constructs a new, empty WeakHashMap2 with the given initial capacity and the given load factor.
      WeakHashMap2​(java.util.Map<K,​V> t)
      Constructs a new WeakHashMap2 with the same mappings as the specified Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all mappings from this map.
      boolean containsKey​(java.lang.Object key)
      Returns true if this map contains a mapping for the specified key.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a Set view of the mappings in this map.
      V get​(java.lang.Object key)
      Returns the value to which this map maps the specified key.
      boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      V put​(K key, V value)
      Updates this map so that the given key maps to the given value.
      V remove​(java.lang.Object key)
      Removes the mapping for the given key from this map, if present.
      K reverseGet​(V value)  
      int size()
      Returns the number of key-value mappings in this map.
      • Methods inherited from class java.util.AbstractMap

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

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • WeakHashMap2

        public WeakHashMap2​(int initialCapacity,
                            float loadFactor)
        Constructs a new, empty WeakHashMap2 with the given initial capacity and the given load factor.
        Parameters:
        initialCapacity - The initial capacity of the WeakHashMap2
        loadFactor - The load factor of the WeakHashMap2
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is less than zero, or if the load factor is nonpositive
      • WeakHashMap2

        public WeakHashMap2​(int initialCapacity)
        Constructs a new, empty WeakHashMap2 with the given initial capacity and the default load factor, which is 0.75.
        Parameters:
        initialCapacity - The initial capacity of the WeakHashMap2
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is less than zero
      • WeakHashMap2

        public WeakHashMap2()
        Constructs a new, empty WeakHashMap2 with the default initial capacity and the default load factor, which is 0.75.
      • WeakHashMap2

        public WeakHashMap2​(java.util.Map<K,​V> t)
        Constructs a new WeakHashMap2 with the same mappings as the specified Map. The WeakHashMap2 is created with an initial capacity of twice the number of mappings in the specified map or 11 (whichever is greater), and a default load factor, which is 0.75.
        Parameters:
        t - the map whose mappings are to be placed in this map.
        Since:
        1.3
    • Method Detail

      • size

        public int size()
        Returns the number of key-value mappings in this map. Note: In contrast with most implementations of the Map interface, the time required by this operation is linear in the size of the map.
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.AbstractMap<K,​V>
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key-value mappings.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.AbstractMap<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.AbstractMap<K,​V>
        Parameters:
        key - The key whose presence in this map is to be tested
      • get

        public V get​(java.lang.Object key)
        Returns the value to which this map maps the specified key. If this map does not contain a value for this key, then return null.
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
        Parameters:
        key - The key whose associated value, if any, is to be returned
      • put

        public V put​(K key,
                     V value)
        Updates this map so that the given key maps to the given value. If the map previously contained a mapping for key then that mapping is replaced and the previous value is returned.
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.AbstractMap<K,​V>
        Parameters:
        key - The key that is to be mapped to the given value
        value - The value to which the given key is to be mapped
        Returns:
        The previous value to which this key was mapped, or null if if there was no mapping for the key
      • reverseGet

        public K reverseGet​(V value)
      • remove

        public V remove​(java.lang.Object key)
        Removes the mapping for the given key from this map, if present.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.AbstractMap<K,​V>
        Parameters:
        key - The key whose mapping is to be removed
        Returns:
        The value to which this key was mapped, or null if there was no mapping for the key
      • clear

        public void clear()
        Removes all mappings from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.AbstractMap<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a Set view of the mappings in this map.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,​V>