WeakHashMap example in Java
WeakHashMap is a HashTable based implementation of the Map Interface. The keys of the WeakHashMap have weak reference and so an entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. Therefore a key will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.
From Java Docs – Java doc
WeakHashMap is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed. This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing.
WeakHashMap example in Java
Lets have a simple example of the WeakHashMap where we will add some data to the map. Then try to make some of the keys “null” and run a garbage collector. Then we will print the WeakHashMap to see the contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.kscodes.collections; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.WeakHashMap; public class WeakHashMapExample { public static void main(String args[]) { // Create a linkedHashMap and add elements to it. String key1 = new String("myKey1"); String key2 = new String("myKey2"); String key3 = new String("myKey3"); String key4 = new String("myKey4"); WeakHashMap<String, String> weakHashMap = new WeakHashMap<>(); weakHashMap.put(key1, "Zebra"); weakHashMap.put(key2, "Apple"); weakHashMap.put(key3, "Dog"); weakHashMap.put(key4, "Ball"); // Set key1 and key3 to null key1 = null; key3 = null; // Display the Elements of the WeakHashMap before Garbage Collector is // called System.out.println("Displaying the Contents of WeakHashMap BEFORE GC"); System.out.println("---------------------------------------"); printWeakHashMap(weakHashMap); // Call Garbage collector System.gc(); // Now again Display the Elements of the WeakHashMap after Garbage // Collector has been called System.out.println("Displaying the Contents of WeakHashMap AFTER GC"); System.out.println("---------------------------------------"); printWeakHashMap(weakHashMap); } public static void printWeakHashMap(WeakHashMap<String, String> weakHashMap) { Set<Entry<String, String>> set = weakHashMap.entrySet(); Iterator<Entry<String, String>> iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapEntry = (Map.Entry) iterator.next(); System.out.print(mapEntry.getKey() + " : " + mapEntry.getValue()); System.out.println(""); } System.out.println("-------------"); System.out.println(""); } } |
Output
As you can see that we made the 2 keys – key1 and key3 = null and then called the System.gc() . After that when we printed the WeakHashMap, we can see only 2 elements present.