What is a WeakHashMap in Java ?

WeakHashMap is a class similar to IdentityHashMap. Internally, it is represented by a Hashtable.

It is not a synchronized class. We can make a WeakHashMap thread safe by using Collections.synchronizedMap() method. An entry in WeakHashMap is automatically removed when it is no longer in ordinary use.

The presence of a mapping for a given key does not prevent the key from being discarded by the garbage collector. WeakHashMap also permits null keys and null values.



You May Interest

What is the Use of Interceptor Design Pattern in Java ?

Why Did Oracle Release a New Version of Java Like Java 8 ?

What are the Thread-safe Classes in Java Collections Framework ?

When is UnsupportedOperationException Thrown in Java ?

Why Do We Use Static Initializers in Java ?