Class WeakIdentityMap<K,V>
- Type Parameters:
K- the key type.V- the value type.
WeakHashMap and IdentityHashMap.
Useful for caches that need to key off of a == comparison instead of a .equals.
This class is not a general-purpose Map implementation! It intentionally
violates Map's general contract, which mandates the use of the equals method when comparing
objects. This class is designed for use only in the rare cases wherein reference-equality
semantics are required.
This implementation was derived from Apache Lucene Lucene
in turn forked this class from from Apache CXF but modified
to not implement the Map interface and without any set views on it, as
those are error-prone and inefficient, if not implemented carefully. The map only contains
Iterator implementations on the values and not-GCed keys. Lucene's implementation also
supports null keys, but those are never weak!
The map supports two modes of operation:
reapOnRead = true: This behaves identical to aWeakHashMapwhere it also cleans up the reference queue on every read operation (get(Object),containsKey(Object),size(),valueIterator()), freeing map entries of already GCed keys.reapOnRead = false: This mode does not callreap()on every read operation. In this case, the reference queue is only cleaned up on write operations (likeput(Object, Object)). This is ideal for maps with few entries where the keys are unlikely be garbage collected, but there are lots ofget(Object)operations. The code can still callreap()to manually clean up the queue without doing a write operation.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all of the mappings from this map.booleancontainsKey(Object key) booleanisEmpty()static <K,V> WeakIdentityMap<K, V> Creates a newWeakIdentityMapbased on aConcurrentHashMap.static <K,V> WeakIdentityMap<K, V> newConcurrentHashMap(boolean reapOnRead) Creates a newWeakIdentityMapbased on aConcurrentHashMap.static <K,V> WeakIdentityMap<K, V> Creates a newWeakIdentityMapbased on a non-synchronizedHashMap.static <K,V> WeakIdentityMap<K, V> newHashMap(boolean reapOnRead) Creates a newWeakIdentityMapbased on a non-synchronizedHashMap.Associates the specified value with the specified key in this map.voidreap()This method manually cleans up the reference queue to remove all garbage collected key/value pairs from the map.Removes the mapping for a key from this weak hash map if it is present.intsize()
-
Method Details
-
newHashMap
Creates a newWeakIdentityMapbased on a non-synchronizedHashMap. The map cleans up the reference queue on every read operation.- Returns:
- the new map.
-
newHashMap
Creates a newWeakIdentityMapbased on a non-synchronizedHashMap.- Parameters:
reapOnRead- controls if the map cleans up the reference queue on every read operation.- Returns:
- the new map.
-
newConcurrentHashMap
Creates a newWeakIdentityMapbased on aConcurrentHashMap. The map cleans up the reference queue on every read operation.- Returns:
- the new map.
-
newConcurrentHashMap
Creates a newWeakIdentityMapbased on aConcurrentHashMap.- Parameters:
reapOnRead- controls if the map cleans up the reference queue on every read operation.- Returns:
- the new map.
-
clear
public void clear()Removes all of the mappings from this map. -
containsKey
- Parameters:
key- the key to check for a mapping.- Returns:
trueif this map contains a mapping for the specified key.
-
get
- Parameters:
key- the key for which to return the associated value.- Returns:
- the value to which the specified key is mapped.
-
put
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.- Parameters:
key- the associate the value with.value- the value to associate with the key.- Returns:
- the previous associated value.
-
isEmpty
public boolean isEmpty()- Returns:
trueif this map contains no key-value mappings.
-
remove
Removes the mapping for a key from this weak hash map if it is present. Returns the value to which this map previously associated the key, ornullif the map contained no mapping for the key. A return value ofnulldoes not necessarily indicate that the map contained.- Parameters:
key- the key to remove.- Returns:
- the previous mapping.
-
size
public int size()- Returns:
- the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.
-
keyIterator
- Returns:
- an iterator over all weak keys of this map. Keys already garbage collected will not be returned. This Iterator does not support removals.
-
valueIterator
- Returns:
- an iterator over all values of this map. This iterator may return values whose key is
already garbage collected while iterator is consumed, especially if
reapOnReadisfalse.
-
reap
public void reap()This method manually cleans up the reference queue to remove all garbage collected key/value pairs from the map. Calling this method is not needed ifreapOnRead = true. Otherwise it might be a good idea to call this method when there is spare time (e.g. from a background thread).- See Also:
-