What is the difference between `WeakHashMap` and a regular `HashMap` with `WeakReference` values?

Java Professional Hard

Java Professional — Hard

What is the difference between `WeakHashMap` and a regular `HashMap` with `WeakReference` values?

Key points

  • WeakHashMap removes entries when keys are garbage collected
  • Regular HashMap with WeakReference values keeps keys alive
  • Key distinction: automatic entry removal in WeakHashMap

Ready to go further?

Related questions