Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class WeakHashMap<K, V>
- Object
- AbstractMap<K, V>
- WeakHashMap
ImplementsMap<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(), keySet() and 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.
Constructors
public WeakHashMap() | |
public WeakHashMap(int initialCapacity) | |
public WeakHashMap(int initialCapacity, float loadFactor) | |
public WeakHashMap(Map<? extends K, ? extends V> m) |
Methods
public int size() | Returns the number of elements in this map. |
public boolean isEmpty() | Returns whether this map is empty. |
public boolean containsKey(Object key) | Returns whether this map contains the specified key. |
public V get(Object key) | Returns the value of the mapping with the specified key. |
public V put(K key, V value) | Maps the specified key to the specified value. |
public V remove(Object key) | Removes a mapping with the specified key from this Map. |
public void clear() | Removes all elements from this map, leaving it empty. |
public Set<Map.Entry<K, V>> entrySet() | A snapshot of the live entries; see the class comment. |
Inherited nested types
Inherited methods
From AbstractMap
containsValue, equals, hashCode, keySet, putAll, toString, values
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()Returns
isEmpty
public boolean isEmpty()Returns
true if this map has no elements, false
otherwise.See also
containsKey
public boolean containsKey(Object key)Parameters
keyObject- the key to search for.
Returns
true if this map contains the specified key,
false otherwise.get
public V get(Object key)Parameters
keyObject- the key.
Returns
null
if no mapping for the specified key is found.put
public V put(K key, V value)Parameters
keyK- the key.
valueV- the value.
Returns
null if there was no mapping.Throws
UnsupportedOperationException- if adding to this map is not supported.
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- if the key or value is
nulland this Map does not supportnullkeys or values.
remove
public V remove(Object key)Parameters
keyObject- the key of the mapping to remove.
Returns
null if no mapping
for the specified key was found.Throws
UnsupportedOperationException- if removing from this map is not supported.
clear
public void clear()Throws
UnsupportedOperationException- if removing from this map is not supported.
See also
entrySet
public Set<Map.Entry<K, V>> entrySet()