Package org.apache.sis.util.collection
Class WeakValueHashMap<K,V>
- Object
-
- AbstractMap<K,V>
-
- WeakValueHashMap<K,V>
-
- Type Parameters:
K- the class of key elements.V- the class of value elements.
- All Implemented Interfaces:
Map<K,V>
public class WeakValueHashMap<K,V> extends AbstractMap<K,V>
A hashtable-based map implementation that uses weak references, leaving memory when an entry is not used anymore. An entry in aWeakValueHashMapwill automatically be removed when its value is no longer in ordinary use. This class is similar to the standardWeakHashMapclass, 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
Cacheinstead.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); } }WeakValueHashMapinstance is not expected to be used in a highly concurrent environment.WeakValueHashMapworks with array keys as one would expect. For example arrays ofint[]are compared using theArrays.equals(int[], int[])method.Thread safetyThe sameWeakValueHashMapinstance 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 onthis.- Since:
- 0.3
- See Also:
WeakHashMap,WeakHashSet,Cache
Defined in the
sis-utilitymodule
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description WeakValueHashMap(Class<K> keyType)Creates a newWeakValueHashMap.WeakValueHashMap(Class<K> keyType, boolean identity)Creates a newWeakValueHashMap, optionally using reference-equality in place of object-equality.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the elements from this map.booleancontainsKey(Object key)Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value)Returnstrueif 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.Vget(Object key)Returns the value to which this map maps the specified key.Vput(K key, V value)Associates the specified value with the specified key in this map.VputIfAbsent(K key, V value)Associates the specified value with the specified key in this map if no value were previously associated.Vremove(Object key)Removes the mapping for this key from this map if present.intsize()Returns the number of key-value mappings in this map.-
Methods inherited from class AbstractMap
clone, equals, hashCode, isEmpty, keySet, putAll, toString, values
-
Methods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
WeakValueHashMap
public WeakValueHashMap(Class<K> keyType)
Creates a newWeakValueHashMap.- Parameters:
keyType- the type of keys in the map.
-
WeakValueHashMap
public WeakValueHashMap(Class<K> keyType, boolean identity)
Creates a newWeakValueHashMap, optionally using reference-equality in place of object-equality. Ifidentityistrue, then two keysk1andk2are considered equal if and only if(k1 == k2)instead than ifk1.equals(k2).Reference-equality semantic is rarely used. See the
IdentityHashMapclass javadoc for a discussion about drawbacks and use cases when reference-equality semantic is useful.- Parameters:
keyType- the type of keys in the map.identity-trueif the map shall use reference-equality in place of object-equality when comparing keys, orfalsefor the standard behavior.- Since:
- 0.4
-
-
Method Detail
-
size
public int size()
Returns the number of key-value mappings in this map.
-
containsKey
public boolean containsKey(Object key)
Returnstrueif this map contains a mapping for the specified key. Null keys are considered never present.- Specified by:
containsKeyin interfaceMap<K,V>- Overrides:
containsKeyin classAbstractMap<K,V>- Parameters:
key- key whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified key.
-
containsValue
public boolean containsValue(Object value)
Returnstrueif this map maps one or more keys to this value. Null values are considered never present.- Specified by:
containsValuein interfaceMap<K,V>- Overrides:
containsValuein classAbstractMap<K,V>- Parameters:
value- value whose presence in this map is to be tested.- Returns:
trueif this map maps one or more keys to this value.
-
get
public V get(Object key)
Returns the value to which this map maps the specified key. Returnsnullif the map contains no mapping for this key. Null keys are considered never present.
-
put
public V put(K key, V value) throws NullArgumentException
Associates the specified value with the specified key in this map. The value is associated using aWeakReference.- Specified by:
putin interfaceMap<K,V>- Overrides:
putin classAbstractMap<K,V>- Parameters:
key- key with which the specified value is to be associated.value- value to be associated with the specified key.- Returns:
- the previous value associated with specified key, or
nullif there was no mapping for key. - Throws:
NullArgumentException- if the key or the value isnull.
-
putIfAbsent
public V putIfAbsent(K key, V value) throws NullArgumentException
Associates the specified value with the specified key in this map if no value were previously associated. If an other value is already associated to the given key, then the map is left unchanged and the current value is returned. Otherwise the specified value is associated to the key using aWeakReferenceandnullis returned.- Parameters:
key- key with which the specified value is to be associated.value- value to be associated with the specified key.- Returns:
- the current value associated with specified key, or
nullif there was no mapping for key. - Throws:
NullArgumentException- if the key or the value isnull.- Since:
- 0.7
-
clear
public void clear()
Removes all of the elements from this map.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Returns a set view of the mappings contained in this map. Each element in this set is aMap.Entry.
-
-