Last Updated: 28 May, 2022
WeakHashMap is a child class of AbstractMap and implements the Map interface in Java. It was introduced in the JDK 1.2 version and is available in the java.util package, which stores only weak references to its keys.
The keys entered into a WeakHashMap are held using weak references. This allows the garbage collector to collect the keys if there are no other strong references to those keys elsewhere in the application.
As we can see from the complete hierarchy above, WeakHashMap extends the AbstractMap class and implements the Map interface.
Here, K is the key Object type and V is the value Object type.
Output
WeakHashMap Data Before calling System.gc()
100 => 11, Ganesh, Sales
200 => 22, Ramesh, Operation
WeakHashMap Data After calling System.gc()
200 => 22, Ramesh, Operation
Constructor | Description |
---|---|
WeakHashMap() | Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75). |
WeakHashMap(int initialCapacity) | 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. |
Java WeakHashMap class has the following methods.
Methods and Descriptions |
---|
void clear() 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. |
Set<Map.Entry<K,V>> entrySet() Returns a Set view of the mappings contained in this map. |
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. |
boolean isEmpty() Returns true if this map contains no key-value mappings. |
Set<K> keySet() 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. |
int size() Returns the number of key-value mappings in this map. |
Collection<V> values() Returns a Collection view of the values contained in this map. |
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/WeakHashMap.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between HashMap and WeakHashMap in Java?
Ans. WeakHashMap is almost similar to the HashMap class in Java. The major difference is that an entry in a WeakHashMap will automatically be removed by the garbage collector when its key is no longer in ordinary use.
Let us examine the differences between the HashMap and WeakHashMap using the table below.
HashMap | WeakHashMap |
---|---|
The stored entry object is not eligible for garbage collection. | An entry in a WeakHashMap will be automatically removed when its key loses all strong and soft references. |
HashMap holds strong references to its key objects. | Weak references to keys are stored in the case of a WeakHashMap. |
The size() method will always return the same value unless we explicitly add or remove entries. | The size() method may return a smaller value as a few entries might be automatically removed by the GC. |
HashMap implements a Serializable interface. | WeakHashMap does not implement the Serializable interface. |
The Cloneable interface is implemented by the HashMap, and its clone() method returns a shallow copy of the HashMap. | It does not implement a clonable interface. |