Java WeakHashMap Class
Java.util package has a WeakHashMap class which is 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. This class supports both null values and the null key. 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. This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. This class is a member of the Java Collections Framework.
The declaration of java.util.WeakHashMap class is:
public class WeakHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>
Here, K and V are the type of key and value respectively maintained by the container.
|S.N||Constructors & Description|
Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).
Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor (0.75).
WeakHashMap(int initialCapacity, float loadFactor)|
Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.
WeakHashMap(Map<? extends K,? extends V> m)|
Constructs a new WeakHashMap with the same mappings as the specified map.
The java.util.WeakHashMap class has a number of methods which are listed below:
|S.N||Methods & Description|
Removes all of the mappings 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 the specified value.
Returns a Set view of the mappings contained in this map.
void forEach(BiConsumer<? super K,? super V> action)|
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
V get(Object key)|
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
Returns true if this map contains no key-value mappings.
Returns a Set view of the keys contained in this map.
V put(K key, V value)|
Associates the specified value with the specified key in this map.
void putAll(Map<? extends K,? extends V> m)|
Copies all of the mappings from the specified map to this map.
V remove(Object key)|
Removes the mapping for a key from this weak hash map if it is present.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)|
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
Returns the number of key-value mappings in this map.
Returns a Collection view of the values contained in this map.
This class inherits the methods of following class: