Class WeakHasherMap<K,V>
- Type Parameters:
K- the type of the map keysV- the type of the map values
- All Implemented Interfaces:
Map<K,V>
WeakHashMap from JDK 1.2.2, that adds a
constructor that takes a Hasher argument. I also replaced uses of hashCode() and
equals() by uses of Hasher, where appropriate. I've changed the name so that this
version is not used accidentally. It is slightly less efficient, in the absence of a Hasher, and has not been as extensively tested.
The original documentation follows.
A hashtable-based Map implementation with weak keys. An entry in a
WeakHashMap will automatically be removed when its key is no longer in ordinary use.
More precisely, the presence of a mapping for a given key will not prevent the key from being
discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed.
When a key 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 has performance characteristics
similar to those of the HashMap class, and has the same efficiency parameters of
initial capacity and load factor.
Like most collection classes, this class is not synchronized. A synchronized WeakHashMap
may be constructed using the Collections.synchronizedMap method.
This class is intended primarily for use with key objects whose equals methods
test for object identity using the == operator. Once such a key is discarded it can
never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap
at some later time and be surprised that its entry has been removed. This class will work
perfectly well with key objects whose equals methods are not based upon object
identity, such as String instances. With such recreatable key objects, however, the
automatic removal of WeakHashMap entries whose keys have been discarded may prove to
be confusing.
The behavior of the WeakHashMap 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 keys at any time, a
WeakHashMap may behave as though an unknown thread is silently removing entries. In
particular, even if you synchronize on a WeakHashMap 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 containsKey method to return true and later false
for a given key, 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 key object in a WeakHashMap is stored indirectly as the referent of a weak
reference. Therefore a key 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.
Implementation note: The value objects in a WeakHashMap are held
by ordinary strong references. Thus care should be taken to ensure that value objects do not
strongly refer to their own keys, either directly or indirectly, since that will prevent the keys
from being discarded. Note that a value object may refer indirectly to its key via the
WeakHashMap itself; that is, a value object may strongly refer to some other key object
whose associated value object, in turn, strongly refers to the key of the first value object.
This problem may be fixed in a future release.
- Since:
- JDK1.2
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new, emptyWeakHashMapwith the default capacity and the default load factor, which is0.75.WeakHasherMap(int initialCapacity) Constructs a new, emptyWeakHashMapwith the given initial capacity and the default load factor, which is0.75.WeakHasherMap(int initialCapacity, float loadFactor) Constructs a new, emptyWeakHashMapwith the given initial capacity and the given load factor.Constructs a new, emptyWeakHashMapwith the default capacity and the default load factor, which is0.75. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all mappings from this map.booleancontainsKey(Object key) Returnstrueif this map contains a mapping for the specified key.entrySet()Returns aSetview of the mappings in this map.@Nullable VReturns the value to which this map maps the specifiedkey.booleanisEmpty()Returnstrueif this map contains no key-value mappings.Updates this map so that the givenkeymaps to the givenvalue.Removes the mapping for the givenkeyfrom this map, if present.intsize()Returns the number of key-value mappings in this map.Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, keySet, putAll, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, keySet, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, values
-
Constructor Details
-
WeakHasherMap
public WeakHasherMap(int initialCapacity, float loadFactor) Constructs a new, emptyWeakHashMapwith the given initial capacity and the given load factor.- Parameters:
initialCapacity- the initial capacity of theWeakHashMaploadFactor- the load factor of theWeakHashMap- Throws:
IllegalArgumentException- If the initial capacity is less than zero, or if the load factor is nonpositive
-
WeakHasherMap
public WeakHasherMap(int initialCapacity) Constructs a new, emptyWeakHashMapwith the given initial capacity and the default load factor, which is0.75.- Parameters:
initialCapacity- the initial capacity of theWeakHashMap- Throws:
IllegalArgumentException- If the initial capacity is less than zero
-
WeakHasherMap
public WeakHasherMap()Constructs a new, emptyWeakHashMapwith the default capacity and the default load factor, which is0.75. -
WeakHasherMap
Constructs a new, emptyWeakHashMapwith the default capacity and the default load factor, which is0.75. TheWeakHashMapuses the specified hasher for hashing keys and comparing them for equality.- Parameters:
h- the Hasher to use when hashing values for this map
-
-
Method Details
-
size
@Pure public int size()Returns the number of key-value mappings in this map. Note: In contrast to most implementations of theMapinterface, the time required by this operation is linear in the size of the map. -
isEmpty
@Pure public boolean isEmpty()Returnstrueif this map contains no key-value mappings. -
containsKey
Returnstrueif this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V> - Parameters:
key- the key whose presence in this map is to be tested
-
get
Returns the value to which this map maps the specifiedkey. If this map does not contain a value for this key, then returnnull. -
put
Updates this map so that the givenkeymaps to the givenvalue. If the map previously contained a mapping forkeythen that mapping is replaced and the previous value is returned.- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V> - Parameters:
key- the key that is to be mapped to the givenvaluevalue- the value to which the givenkeyis to be mapped- Returns:
- the previous value to which this key was mapped, or
nullif if there was no mapping for the key
-
remove
Removes the mapping for the givenkeyfrom this map, if present. -
clear
public void clear()Removes all mappings from this map. -
entrySet
Returns aSetview of the mappings in this map.
-