PrepZone Logo
PrepZone
Back to Java

Java · Hard

How are WeakHashMap and SoftReference used in caching?

JavaCollectionsJVM

Answer preview

WeakHashMap — Keys are held via WeakReference. When a key has no strong references elsewhere, the GC reclaims it and the entry is automatically removed. Ideal for canonical maps and metadata caches.…