Monday, 3 September 2018

WeakHashMap and IdentityHashMap


WeakHashMap

Elements in a weak hashmap can be reclaimed or removed by the garbage collector if there are no other strong references to the key object, this makes them useful for caches/lookup storage. WeakHashMap is the Hash table-based implementation of the Map interface, with weak keys
Suppose, we want to build a cache that keeps big image objects as values, and placing image names as keys. If we are using a simple HashMap then that will not be a good choice because the value objects may grab a lot of memory. 
For this scenario, we want a Map implementation that allows GC to automatically delete unused objects. When a key of a big image object is not in use in our application in any place, that entry will be deleted from memory. WeakHashMap is available for this type of scenario's.

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapExample {
      
public static void main(String[] args) {
            Map<String, String> hashMap = new HashMap<>();
            Map<String, String> weakHashMap = new WeakHashMap<>();

            String hashMapKey = new String("HashMapkey");
            String weakHashMapKey = new String("WeakHashMapkey");

            hashMap.put(hashMapKey, "Shivam");
            weakHashMap.put(weakHashMapKey, "Gaurav");

            System.out.println("Before Garbage Collection, HashMap value=" + hashMap.get("HashMapkey")
                        + " and WeakHashMap value=" + weakHashMap.get("WeakHashMapkey"));

            hashMapKey = null;
            weakHashMapKey = null;

            System.gc();

            System.out.println("After Garbage Collection, HashMap value=" + hashMap.get("HashMapkey")
                        + " and WeakHashMap value=" + weakHashMap.get("WeakHashMapkey"));
      }
}

Output 
Before Garbage Collection, HashMap value=Shivam and WeakHashMap value=Gaurav
After Garbage Collection, HashMap value=Shivam and WeakHashMap value=null
  

IdentityHashMap


IdentityHashMap uses equality operator (==) for comparing the keys. So, when you put any Key Value pair in it the Key Object is compared using == operator. This Map class intentionally violates Map’s general contract, which mandates the use of the equals method when comparing objects.
We can use this class when the user requires the objects to be compared via reference.

import java.util.Map;
import java.util.HashMap;
import java.util.IdentityHashMap;

public class IdentityHashMapExample
{
    public static void main(String[] args)
    {
        Map<String, Integer> hashmap = new HashMap<>();
        Map<String, Integer> identityhashmap = new IdentityHashMap<>();

        hashmap.put("Shivam",5);
        hashmap.put(new String("Shivam"),6);
       
        identityhashmap.put("Gaurav",30);
        identityhashmap.put(new String("Gaurav"),35);
        
        System.out.println("HashMap Size is:"+hashmap.size()+", Map Objects are:"+hashmap.toString());
        
        System.out.println("IdentityHashMap Size is:"+identityhashmap.size()+", Identity HashMap Objects are:"+identityhashmap.toString());

        
    }
}

Output

HashMap Size is:1, Map Objects are:{Shivam=6}
IdentityHashMap Size is:2, Identity HashMap Objects are:{Gaurav=30, Gaurav=35}

No comments:

Post a Comment