Class WeakMap<K,​V>

  • All Implemented Interfaces:
    java.util.Map

    public final class WeakMap<K,​V>
    extends java.lang.Object
    implements java.util.Map
    A hash map that holds values of type WeakReference.

    Like HashMap, this implementation provides all of the optional map operations, and permits the null key.

    Also like HashMap, this implementation is not synchronized. If multiple threads share an instance, and at least one of them executes any modifying operations on the WeakMap, they have to use external synchronization.

    Unlike other map implementations, WeakMap is asymmetric in that put expects the given value to be a plain object that is then wrapped in a WeakReference, while the occurrences of values in all other methods (containsValue, entrySet, equals, get, hashCode, remove, values, and also the return value of put) expect already wrapped instances of WeakReference. That is, after weakMap.put("key", o), weakMap.get("key").equals(o) does not work as naïvely expected; neither does weakMap1.putAll(weakMap2).

    At an arbitrary time after the WeakReference value of an entry has been cleared by the garbage collector, the entry is automatically removed from the map.

    Values placed into a WeakMap may optionally support the DisposeNotifier interface. For those that do, the associated WeakReference wrappers are automatically cleared as soon as the values are disposed.

    Note that this class does not actually implement the Map interface properly, the type of the return value of the entrySet and values methods is wrong, but the "implements Map" is retained for backward compatibility.
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakMap()
      Constructs an empty WeakMap.
      WeakMap​(java.util.Map<K,​V> m)
      Constructs a new WeakMap with the same mappings as the specified Map.
    • Method Summary

      All Methods Static 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.
      boolean containsValue​(java.lang.Object value)
      Returns true if this map maps one or more keys to the specified value.
      java.util.Set entrySet()
      Returns a collection view of the mappings contained in this map.
      boolean equals​(java.lang.Object o)  
      java.lang.ref.WeakReference<V> get​(java.lang.Object key)
      Returns the value to which the specified key is mapped in this map, or null if the map contains no mapping for this key.
      static <T> T getValue​(java.lang.Object ref)
      Returns the referent of a WeakReference, silently handling a null argument.
      int hashCode()  
      boolean isEmpty()
      Returns true if this map contains no key–value mappings.
      java.util.Set<K> keySet()
      Returns a view of the keys contained in this map.
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Associates the specified value with the specified key in this map.
      void putAll​(java.util.Map m)
      Copies all of the mappings from the specified map to this map.
      java.lang.Object remove​(java.lang.Object key)
      Removes the mapping for this key from this map if present.
      int size()
      Returns the number of key–value mappings in this map.
      java.util.Collection<java.lang.ref.WeakReference<V>> values()
      Returns a collection view of the values contained in this map.
      • Methods inherited from class java.lang.Object

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

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

      • WeakMap

        public WeakMap()
        Constructs an empty WeakMap.
      • WeakMap

        public WeakMap​(java.util.Map<K,​V> m)
        Constructs a new WeakMap with the same mappings as the specified Map.
        Parameters:
        m - the map whose mappings are to be placed in this map
    • Method Detail

      • size

        public int size()
        Returns the number of key–value mappings in this map.

        This is a non-modifying operation.

        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        the number of key–value mappings in this map
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key–value mappings.

        This is a non-modifying operation.

        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true if this map contains no key–value mappings
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if this map contains a mapping for the specified key.

        This is a non-modifying operation.

        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - the key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns true if this map maps one or more keys to the specified value.

        This is a non-modifying operation.

        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - the value whose presence in this map is to be tested
        Returns:
        true if this map maps one or more keys to the specified value
      • get

        public java.lang.ref.WeakReference<V> get​(java.lang.Object key)
        Returns the value to which the specified key is mapped in this map, or null if the map contains no mapping for this key.

        This is a non-modifying operation.

        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which this map maps the specified key, or null if the map contains no mapping for this key
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Associates the specified value with the specified key in this map.

        This is a modifying operation.

        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - the key with which the specified value is to be associated
        value - the value to be associated with the specified key. This must be a plain object, which is then wrapped in a WeakReference.
        Returns:
        previous value associated with the specified key, or null if there was no mapping for the key
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removes the mapping for this key from this map if present.

        This is a modifying operation.

        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - the key whose mapping is to be removed from the map
        Returns:
        previous value associated with the specified key, or null if there was no mapping for the key
      • putAll

        public void putAll​(java.util.Map m)
        Copies all of the mappings from the specified map to this map.

        This is a modifying operation.

        Specified by:
        putAll in interface java.util.Map<K,​V>
        Parameters:
        m - mappings to be stored in this map. The values of those mappings must be plain objects, which are then wrapped in instances of WeakReference.
      • clear

        public void clear()
        Removes all mappings from this map.

        This is a modifying operation.

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

        public java.util.Set<K> keySet()
        Returns a view of the keys contained in this map.

        This is a non-modifying operation.

        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the keys contained in this map
      • values

        public java.util.Collection<java.lang.ref.WeakReference<V>> values()
        Returns a collection view of the values contained in this map.

        This is a non-modifying operation.

        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        a collection view of the values contained in this map
      • entrySet

        public java.util.Set entrySet()
        Returns a collection view of the mappings contained in this map.

        This is a non-modifying operation.

        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        a collection view of the mappings contained in this map
      • equals

        public boolean equals​(java.lang.Object o)
        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
      • getValue

        public static <T> T getValue​(java.lang.Object ref)
        Returns the referent of a WeakReference, silently handling a null argument.

        This static method is useful to wrap around the return values of methods like get.

        Parameters:
        ref - must be either an instance of WeakReference or null
        Returns:
        the referent of the specified WeakReference, or null if ref is null