Class WeakHashMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.WeakHashMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class WeakHashMap<K,V> extends AbstractMap<K,V>

java.util.WeakHashMap: a map that does not keep its keys alive. An entry disappears once nothing else refers to its key, which is what lets desktop code hang data off a component -- a layout manager's cached constraints, say -- without ever removing it.

Keys are held through the device's WeakReference and compared with equals, as the JDK's are. Entries whose key is gone are dropped by the next change to the map, by size(), and by reads too, in a sweep that runs once per roughly as many reads as the map has keys -- so a cache that is only ever read still lets go of its values, at an amortized constant cost per read. There is no reference queue on a device, so nothing is dropped in between, and size() counts only the entries still alive.

entrySet(), AbstractMap.keySet() and AbstractMap.values() are snapshots taken when they are called: removing through them, or through their iterators, does not change the map. The JDK's are live views; code that edits a weak map through a view is rare enough that the simpler contract is kept.

Like every shared JDK class here it is not synchronized, and neither is the JDK's.

  • Constructor Details

    • WeakHashMap

      public WeakHashMap()
    • WeakHashMap

      public WeakHashMap(int initialCapacity)
    • WeakHashMap

      public WeakHashMap(int initialCapacity, float loadFactor)
    • WeakHashMap

      public WeakHashMap(Map<? extends K, ? extends V> m)
  • Method Details

    • size

      public int size()
      Description copied from class: AbstractMap

      Returns the number of elements in this map.

      Returns

      the number of elements in this map.

      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
    • isEmpty

      public boolean isEmpty()
      Description copied from class: AbstractMap

      Returns whether this map is empty.

      Returns
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class AbstractMap<K,V>
      Returns:

      true if this map has no elements, false otherwise.

      See also
      • #size()
    • containsKey

      public boolean containsKey(Object key)
      Description copied from class: AbstractMap

      Returns whether this map contains the specified key.

      Parameters
      • key: the key to search for.
      Returns
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Returns:
      true if this map contains the specified key, false otherwise.
    • get

      public V get(Object key)
      Description copied from class: AbstractMap

      Returns the value of the mapping with the specified key.

      Parameters
      • key: the key.
      Returns
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Returns:
      the value of the mapping with the specified key, or null if no mapping for the specified key is found.
    • put

      public V put(K key, V value)
      Description copied from class: AbstractMap

      Maps the specified key to the specified value.

      Parameters
      • key: the key.

      • value: the value.

      Returns
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Returns:

      the value of any previous mapping with the specified key or null if there was no mapping.

      Throws
      • UnsupportedOperationException: if adding to this map is not supported.

      • ClassCastException: @throws ClassCastException if the class of the key or value is inappropriate for this map.

      • IllegalArgumentException: if the key or value cannot be added to this map.

      • NullPointerException: @throws NullPointerException if the key or value is null and this Map does not support null keys or values.

    • remove

      public V remove(Object key)
      Description copied from class: AbstractMap

      Removes a mapping with the specified key from this Map.

      Parameters
      • key: the key of the mapping to remove.
      Returns
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      Returns:

      the value of the removed mapping or null if no mapping for the specified key was found.

      Throws
      • UnsupportedOperationException: if removing from this map is not supported.
    • clear

      public void clear()
      Description copied from class: AbstractMap

      Removes all elements from this map, leaving it empty.

      Throws
      • UnsupportedOperationException: if removing from this map is not supported.
      See also
      • #isEmpty()

      • #size()

      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      A snapshot of the live entries; see the class comment.
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>