1. Overview
In this tutorial, We'll be talking about
WeakHashMap in java and it is placed in
java.util package.
WeakHashMap is implemented based on Hashtable with weak keys. Map internally is built on Entry objects. When adding a key-value pair, it will be stored in the Entry object. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. So, once the key is deleted from the map it is eligible for Garbage Collection and we can not prevent from running GC on it. This class completely behaves differently than other Map implementations such as HashMap, LinkedHashMap, etc. And also it has efficiency parameters of initial capacity and load factor as same as HashMap.
AbstractMap is the superclass for WeakHashMap and it implements Map interface. So, it acquires all its default methods. WeakHashMap is designed to store null values as key and value.
We should understand the basic terms Strong reference, Soft Reference, and Weak Reference.
Next in this article, We will see example programs on each method of this class.