Java Utility Library

Java.util.WeakHashMap Class



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.

Class declaration

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.

Class Constructors

S.NConstructors & Description
1. WeakHashMap()
Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).
2. WeakHashMap(int initialCapacity)
Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor (0.75).
3. WeakHashMap(int initialCapacity, float loadFactor)
Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.
4. WeakHashMap(Map<? extends K,? extends V> m)
Constructs a new WeakHashMap with the same mappings as the specified map.

java.util.WeakHashMap Methods

The java.util.WeakHashMap class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void clear()
Removes all of the mappings from this map.
2. boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
3. boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
4. Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
5. 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.
6. 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.
7. boolean isEmpty()
Returns true if this map contains no key-value mappings.
8. Set<K> keySet()
Returns a Set view of the keys contained in this map.
9. V put(K key, V value)
Associates the specified value with the specified key in this map.
10. void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.
11. V remove(Object key)
Removes the mapping for a key from this weak hash map if it is present.
12. 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.
13. int size()
Returns the number of key-value mappings in this map.
14. Collection<V> values()
Returns a Collection view of the values contained in this map.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractMap<K,V>