HashMap Vs WeakHashMap

HashMap Vs WeakHashMap

While working with Collection framework I came to know about WeakHashMap. The first question came to my mind, Java has already lots of concrete classes for Map interface then why another class WeakHashMap. There are some basic difference between HashMap and WeakHashMap. For more about WeakHashMap you can refer here. In interviews, it is very common to ask question for WeakHashMap Vs HashMap. Here are some differences between HashMap and WeakHashMap.

Difference between HashMap and WeakHashMap.

[table “” not found /]

There are some similarity between HashMap and WeakHashMap.

  1. Both classes are not serialized.
  2. Both classes permit null as key and value.
  3. Default initial capacity and load factor is same for both classes.(initialCapacity : 16, loadFactor : 0.75).
  4. Insertion order is not maintain in HashMap and WeakHashMap.

Example

Output

Leave a Reply

Your email address will not be published. Required fields are marked *