K
- The class of key elements.V
- The class of value elements.public class WeakValueHashMap<K,V> extends AbstractMap<K,V>
WeakValueHashMap
will automatically be removed when its value is no longer in ordinary use. This class is
similar to the standard WeakHashMap
class, except that weak references
apply to values rather than keys.
Note that this class is not a cache, because the entries are discarded
as soon as the garbage collector determines that they are no longer in use. If caching
service are wanted, or if concurrency are wanted, consider using Cache
instead.
This class is convenient for avoiding the creation of duplicated elements, as in the example below:
In the above example, the calculation of a new value needs to be fast because it is performed inside a synchronized statement blocking all other access to the map. This is okay if that particularK key = ... V value; synchronized (map) { value = map.get(key); if (value != null) { value = ...; // Create the value here. map.put(key, value); } }
WeakValueHashMap
instance
is not expected to be used in a highly concurrent environment.
WeakValueHashMap
works with array keys as one would expect. For example arrays of int[]
are
compared using the Arrays.equals(int[], int[])
method.
WeakValueHashMap
instance can be safely used by many threads without synchronization on the part
of the caller. But if a sequence of two or more method calls need to appear atomic from other threads perspective,
then the caller can synchronize on this
.WeakHashMap
,
WeakHashSet
,
Cache
Defined in the sis-utility
module
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
WeakValueHashMap(Class<K> keyType)
Creates a new
WeakValueHashMap . |
WeakValueHashMap(Class<K> keyType,
boolean identity)
Creates a new
WeakValueHashMap , optionally using reference-equality in place of object-equality. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all of the elements 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 this value. |
Set<Map.Entry<K,V>> |
entrySet()
Returns a set view of the mappings contained in this map.
|
V |
get(Object key)
Returns the value to which this map maps the specified key.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
V |
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.
|
public WeakValueHashMap(Class<K> keyType)
WeakValueHashMap
.keyType
- The type of keys in the map.public WeakValueHashMap(Class<K> keyType, boolean identity)
WeakValueHashMap
, optionally using reference-equality in place of object-equality.
If identity
is true
, then two keys k1
and k2
are considered equal if and
only if (k1 == k2)
instead than if k1.equals(k2)
.
Reference-equality semantic is rarely used. See the IdentityHashMap
class javadoc
for a discussion about drawbacks and use cases when reference-equality semantic is useful.
keyType
- The type of keys in the map.identity
- true
if the map shall use reference-equality in place of object-equality
when comparing keys, or false
for the standard behavior.public int size()
public boolean containsKey(Object key)
true
if this map contains a mapping for the specified key.
Null keys are considered never present.containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
key
- key whose presence in this map is to be tested.true
if this map contains a mapping for the specified key.public boolean containsValue(Object value)
true
if this map maps one or more keys to this value.
Null values are considered never present.containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
value
- value whose presence in this map is to be tested.true
if this map maps one or more keys to this value.public V get(Object key)
null
if the map contains no mapping for this key.
Null keys are considered never present.public V put(K key, V value) throws NullArgumentException
WeakReference
.put
in interface Map<K,V>
put
in class AbstractMap<K,V>
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.null
if there was no mapping for key.NullArgumentException
- if the key or the value is null
.public void clear()
Copyright © 2010–2014 The Apache Software Foundation. All rights reserved.