Class WeakHashMap<K,V>
- All Implemented Interfaces:
Map<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.
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionWeakHashMap(int initialCapacity) WeakHashMap(int initialCapacity, float loadFactor) WeakHashMap(Map<? extends K, ? extends V> m) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all elements from this map, leaving it empty.booleancontainsKey(Object key) Returns whether this map contains the specified key.entrySet()A snapshot of the live entries; see the class comment.Returns the value of the mapping with the specified key.booleanisEmpty()Returns whether this map is empty.Maps the specified key to the specified value.Removes a mapping with the specified key from this Map.intsize()Returns the number of elements in this map.Methods inherited from class AbstractMap
containsValue, equals, hashCode, keySet, putAll, toString, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
WeakHashMap
public WeakHashMap() -
WeakHashMap
public WeakHashMap(int initialCapacity) -
WeakHashMap
public WeakHashMap(int initialCapacity, float loadFactor) -
WeakHashMap
-
-
Method Details
-
size
public int size()Description copied from class:AbstractMapReturns the number of elements in this map.
Returns
the number of elements in this map.
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractMapReturns whether this map is empty.
Returns
-
containsKey
Description copied from class:AbstractMapReturns whether this map contains the specified key.
Parameters
key: the key to search for.
Returns
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Returns:
trueif this map contains the specified key,falseotherwise.
-
get
Description copied from class:AbstractMapReturns the value of the mapping with the specified key.
Parameters
key: the key.
Returns
-
put
Description copied from class:AbstractMapMaps the specified key to the specified value.
Parameters
-
key: the key. -
value: the value.
Returns
- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Returns:
the value of any previous mapping with the specified key or
nullif 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 isnulland this Map does not supportnullkeys or values.
-
-
-
remove
Description copied from class:AbstractMapRemoves a mapping with the specified key from this Map.
Parameters
key: the key of the mapping to remove.
Returns
-
clear
public void clear()Description copied from class:AbstractMapRemoves all elements from this map, leaving it empty.
Throws
UnsupportedOperationException: if removing from this map is not supported.
See also
-
#isEmpty()
-
#size()
-
entrySet
-