K - Key typeV - Value typepublic class WeakValueMap<K,V>
extends java.util.AbstractMap<K,V>
A map implementation with weak values.
An entry in a WeakValueMap will automatically be removed when its value is no longer in ordinary use. More precisely, the presence of a mapping for a given value will not prevent the value from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a value has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.
Both null values and the null key are
supported.
This class is intended primarily for use with value objects whose equals methods test for object identity using the == operator.
The behavior of the WeakValueMap class depends in part upon the actions of the garbage collector, so several familiar (though not required) Map invariants do not hold for this class. Because the garbage collector may discard values at any time, a WeakValueMap may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a WeakValueMap instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the containsValue method to return true and later false for a given value, for the get method to return a value for a given key but later return null, for the put method to return null and the remove method to return false for a key that previously appeared to be in the map, and for successive examinations of the key set, the value set, and the entry set to yield successively smaller numbers of elements.
Each value object in a WeakValueMap is stored indirectly as the referent of a weak reference. Therefore a value will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.
This class implementation has been highly inspired on the SoftHashMap implementation by Dr. Heinz Kabutz.
| Constructor and Description |
|---|
WeakValueMap(java.lang.Class<? extends java.util.Map> mapType)
Create a WeakValue map instance backed by a Map of the given type.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
V |
get(java.lang.Object key) |
V |
put(K key,
V value)
Here we put the key, value pair into the Map using
a WeakValue object.
|
V |
remove(java.lang.Object key) |
int |
size() |
public WeakValueMap(java.lang.Class<? extends java.util.Map> mapType)
Create a WeakValue map instance backed by a Map of the given type.
mapType - the type of the real map to useCopyright © 2011 OW2 Consortium. All Rights Reserved.