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.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final HashMap | map
Declare the map as WeakReference instead of Entry because it makes the return type signatures of values() and keySet() cleaner. |
| private final ReferenceQueue |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | |
| public boolean | Returns: true if this map contains a mapping for the
specified keythe key whose presence in this map is to be tested key)Implements java. Returns |
| public boolean | Returns: true if this map maps one or more keys to the
specified valuethe value whose presence in this map is to be tested value)Implements java. Returns |
| public Set | Returns: a collection view of the mappings contained in this mapImplements java. Returns a collection view of the mappings contained in this map. |
| public boolean | equals(Object o)
Overrides java. Implements java. Compares the specified object with this map for equality. |
| public WeakReference | Returns: the value to which this map maps the specified key, ornull if the map contains no mapping for this keythe key whose associated value is to be returned key)Implements java. Returns the value to which the specified key is mapped in this map, or
|
| public static <T> T | |
| public int | hashCode()
Overrides java. Implements java. Returns the hash code value for this map. |
| public boolean | Returns: true if this map contains no key–value
mappingsImplements java. Returns |
| public Set | Returns: a set view of the keys contained in this mapImplements java. Returns a view of the keys contained in this map. |
| public Object | Returns: previous value associated with the specified key, ornull if there was no mapping for the keythe key with which the specified value is to be associated key, Object the value to be associated with the specified key. This
must be a plain object, which is then wrapped in a
value)WeakReference.Implements java. Associates the specified value with the specified key in this map. |
| public void | putAll(Map<K, V>
mappings to be stored in this map. The values of those mappings
must be plain objects, which are then wrapped in instances of
m)WeakReference.Implements java. Copies all of the mappings from the specified map to this map. |
| public Object | Returns: previous value associated with the specified key, ornull if there was no mapping for the keythe key whose mapping is to be removed from the map key)Implements java. Removes the mapping for this key from this map if present. |
| public int | Returns: the number of key–value mappings in this mapImplements java. Returns the number of key–value mappings in this map. |
| public Collection | Returns: a collection view of the values contained in this mapImplements java. Returns a collection view of the values contained in this map. |
| map | back to summary |
|---|---|
| private final HashMap<K, WeakReference<V>> map Declare the map as WeakReference instead of Entry because it makes the return type signatures of values() and keySet() cleaner. | |
| queue | back to summary |
|---|---|
| private final ReferenceQueue<V> queue | |
| WeakMap | back to summary |
|---|---|
| public WeakMap() Constructs an empty | |
| WeakMap | back to summary |
|---|---|
| public WeakMap(Map<K, V> m) Constructs a new
| |
| cleanUp | back to summary |
|---|---|
| private void cleanUp() cleanUp() must only be called from within modifying methods. Otherwise, the implementations of entrySet, keySet and values would break (Specifically, iterating over the collections returned by those methods), as non-modifying methods might modify the underlying map.
| |
| clear | back to summary |
|---|---|
| public void clear() Implements java. Removes all mappings from this map. This is a modifying operation. | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(Object key) Implements java. Returns This is a non-modifying operation.
| |
| containsValue | back to summary |
|---|---|
| public boolean containsValue(Object value) Implements java. Returns This is a non-modifying operation.
| |
| entrySet | back to summary |
|---|---|
| public Set Implements java. Returns a collection view of the mappings contained in this map. This is a non-modifying operation.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Implements java. Doc from java. Compares the specified object with this map for equality. Returns
| |
| get | back to summary |
|---|---|
| public WeakReference Implements java. Returns the value to which the specified key is mapped in this map, or
This is a non-modifying operation.
| |
| getValue | back to summary |
|---|---|
| public static <T> T getValue(Object ref) Returns the referent of a This static method is useful to wrap around the return values of
methods like
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this map. The hash code of a map is
defined to be the sum of the hash codes of each entry in the map's
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Implements java. Returns This is a non-modifying operation.
| |
| keySet | back to summary |
|---|---|
| public Set Implements java. Returns a view of the keys contained in this map. This is a non-modifying operation.
| |
| put | back to summary |
|---|---|
| public Object put(Object key, Object value) Implements java. Associates the specified value with the specified key in this map. This is a modifying operation.
| |
| putAll | back to summary |
|---|---|
| public void putAll(Map<K, V> m) Implements java. Copies all of the mappings from the specified map to this map. This is a modifying operation.
| |
| remove | back to summary |
|---|---|
| public Object remove(Object key) Implements java. Removes the mapping for this key from this map if present. This is a modifying operation. | |
| size | back to summary |
|---|---|
| public int size() Implements java. Returns the number of key–value mappings in this map. This is a non-modifying operation.
| |
| values | back to summary |
|---|---|
| public Collection Implements java. Returns a collection view of the values contained in this map. This is a non-modifying operation.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final K |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | notifyDispose(DisposeNotifier source)
Implements com. Callback fired by a |
| key | back to summary |
|---|---|
| private final K key | |
| Entry | back to summary |
|---|---|
| private Entry(K key, V value, ReferenceQueue<V> queue) | |
| notifyDispose | back to summary |
|---|---|
| public void notifyDispose(DisposeNotifier source) Implements com. Doc from com. Callback fired by a | |