It's no surprise that this one pops up often in Java collections programming interview questions. WeakHashMap is using the WeakReference class, first, we need to understand how WeakReference works, WeakReference is a class which holds the reference and GC captured that reference eagerly and does the garbage collection. Below is the code snippet for WeakReference. Integer prime = 1;
WeakReference soft = new WeakReference(prime);
prime = null; In this case, prime no gets garbage collected easily. The WeakHashMap has utilized the WeakReference classes, below code structure of WeakHashMap shows about the utilizing of WeakReference. The WeakHashMap extends the WeakReference classes private static class Entry extends WeakReference