Hashmap vs WeakHashMap in Java
Java.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair Output: WeakHashMap WeakHashMap is an implementation of the Map interface. WeakHashMap is almost same as HashMap except in case of WeakHashMap, if object is specified as key doesn’t contain any references- it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Collector dominates over WeakHashMap. Output: Some more important differences between Hashmap and WeakHashmap: This article is contributed by Rishabh Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Even though the object is specified as key in hashmap, it does not have any reference and it is not eligible for garbage collection if it is associated with HashMap i.e. HashMap dominates over Garbage Collector.// Java program to illustrate // Hashmap import java.util.*;class HashMapDemo{ public static void main(String args[])throws Exception { HashMap m = new HashMap(); Demo d = new Demo(); // puts an entry into HashMap m.put(d," Hi "); System.out.println(m); d = null; // garbage collector is called System.gc(); //thread sleeps for 4 sec Thread.sleep(4000); System.out.println(m); } } class Demo { public String toString() { return "demo"; } // finalize method public void finalize() { System.out.println("Finalize method is called"); }}
{demo=Hi}
{demo=Hi}
// Java program to illustrate // WeakHashmap import java.util.*;class WeakHashMapDemo{ public static void main(String args[])throws Exception { WeakHashMap m = new WeakHashMap(); Demo d = new Demo(); // puts an entry into WeakHashMap m.put(d," Hi "); System.out.println(m); d = null; // garbage collector is called System.gc(); // thread sleeps for 4 sec Thread.sleep(4000); . System.out.println(m); }} class Demo{ public String toString() { return "demo"; } // finalize method public void finalize() { System.out.println("finalize method is called"); }}
{demo = Hi}
finalize method is called
{ }
Strong References: This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. In HashMap, key objects have strong reference.
In WeakHashmap, wWhen a key is discarded then its entry is automatically removed from the map , in other words, garbage collected.
WeakHashMap does not implement Cloneable interface , it only implements Map interface. Hence , there is no clone() method in the WeakHashMap class.



