public final class WeakMap<K,V> extends Object implements Map
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.
| Constructor and Description |
|---|
WeakMap()
Constructs an empty
WeakMap. |
WeakMap(Map<K,V> m)
Constructs a new
WeakMap with the same mappings as the
specified Map. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all mappings from this map.
|
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the
specified key. |
boolean |
containsValue(Object value)
Returns
true if this map maps one or more keys to the
specified value. |
Set |
entrySet()
Returns a collection view of the mappings contained in this map.
|
boolean |
equals(Object o) |
WeakReference<V> |
get(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(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. |
Set<K> |
keySet()
Returns a view of the keys contained in this map.
|
Object |
put(Object key,
Object value)
Associates the specified value with the specified key in this map.
|
void |
putAll(Map m)
Copies all of the mappings from the specified map to this map.
|
Object |
remove(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.
|
Collection<WeakReference<V>> |
values()
Returns a collection view of the values contained in this map.
|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic int size()
This is a non-modifying operation.
public boolean isEmpty()
true if this map contains no key–value
mappings.
This is a non-modifying operation.
public boolean containsKey(Object key)
true if this map contains a mapping for the
specified key.
This is a non-modifying operation.
containsKey in interface Mapkey - the key whose presence in this map is to be testedtrue if this map contains a mapping for the
specified keypublic boolean containsValue(Object value)
true if this map maps one or more keys to the
specified value.
This is a non-modifying operation.
containsValue in interface Mapvalue - the value whose presence in this map is to be testedtrue if this map maps one or more keys to the
specified valuepublic WeakReference<V> get(Object key)
null if the map contains no mapping for this key.
This is a non-modifying operation.
public Object put(Object key, Object value)
This is a modifying operation.
put in interface Mapkey - the key with which the specified value is to be associatedvalue - the value to be associated with the specified key. This
must be a plain object, which is then wrapped in a
WeakReference.null if there was no mapping for the keypublic Object remove(Object key)
This is a modifying operation.
public void putAll(Map m)
This is a modifying operation.
public void clear()
This is a modifying operation.
public Set<K> keySet()
This is a non-modifying operation.
public Collection<WeakReference<V>> values()
This is a non-modifying operation.
public Set entrySet()
This is a non-modifying operation.
public boolean equals(Object o)
public int hashCode()
public static <T> T getValue(Object ref)
WeakReference, silently handling a
null argument.
This static method is useful to wrap around the return values of
methods like get.
ref - must be either an instance of WeakReference or
nullWeakReference, or
null if ref is null