Hashmap vs WeakHashMap in Java Last Updated : 29 Sep, 2021 Comments Improve Suggest changes Like Article Like Report HashMap Java.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair. 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 // 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"); } } Output: {demo=Hi} {demo=Hi}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. Java // 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"); } } Output: {demo = Hi} finalize method is called { } Some more important differences between Hashmap and WeakHashmap: Strong vs Weak References: Weak Reference Objects are not the default type/class of Reference Object and they should be explicitly specified while using them. This type of reference is used in WeakHashMap to reference the entry objects. 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 references. Role of Garbage Collector: Garbage Collected : In HashMap , entry object(entry object stores key-value pairs) is not eligible for garbage collection i.e Hashmap is dominant over Garbage Collector. In WeakHashmap, When a key is discarded then its entry is automatically removed from the map, in other words, garbage collected.Clone method Implementation: HashMap implements Cloneable interface. WeakHashMap does not implement Cloneable interface, it only implements Map interface. Hence, there is no clone() method in the WeakHashMap class. Comment More infoAdvertise with us Next Article WeakHashMap get() Method in Java R Rishabh Patel 1 Improve Article Tags : Java Practice Tags : Java Similar Reads WeakHashMap Class in Java WeakHashMap is an implementation of the Map interface. WeakHashMap is almost the same as HashMap except in the case of WeakHashMap if the object is specified as the key doesnât contain any references- it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Co 6 min read WeakHashMap clear() Method in Java The java.util.WeakHashMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified map. Syntax: Weak_Hash_Map.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illus 2 min read WeakHashMap values() Method in Java The java.util.WeakHashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: Weak_Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The met 2 min read WeakHashMap get() Method in Java The java.util.WeakHashMap.get() method of WeakHashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: WeakHashMap.get(Object key_element) Parameter: The method takes one par 2 min read HashMap and TreeMap in Java HashMap and TreeMap are part of collection framework. HashMapjava.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair<Key, Value>. HashMap<K, V> hmap = new HashMap<K, V>(); Let us consider below example where we have to count occurrences 5 min read WeakHashMap size() method in Java The java.util.WeakHashMap.size() method of WeakHashMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. Syntax: WeaK_Hash_Map.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the 2 min read Like