Monday, 10 October 2016

What is WeakHashMap In Java ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed about
What is EnumMap In Java ?
Here, we learn What is WeakHashMap In Java.But before starting we must know what is Map Interface In Java.?
Class WeakHashMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

All Implemented Interfaces:

Declaration of WeakHashMap:
public class WeakHashMap<K,V>
implements Map<K,V>

WeakHashMap is a implementation class of Map Interface.It belongs to java.util.*; package.
An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
When a key has been discarded its entry is effectively removed from the map, so this class behaves some what differently from other Map implementations.

Both null keys and the null values are supported. This class has performance similar to those of the HashMap class, and has the same efficiency parameters of initial capacity and load factor.

The behavior of the WeakHashMap class depends upon the garbage collector,Because the garbage collector may discard keys at any time.It is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the containsKey method to return true and later false for a given key, for the get method to return a value for a given key but later return null, for the put method to return null and the remove method to return false for a key that previously appeared to be in the map.

WeakHashMap implementation is not synchronized.But we can synchronized using the Collections.synchronizedMap method.WeakHashMap is an example of fail-fast.

This class is a member of the Java Collections Framework.

Constructor and Description :-

WeakHashMap()
Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

WeakHashMap(int initialCapacity)
Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor (0.75).

WeakHashMap(int initialCapacity, float loadFactor)
Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.

WeakHashMap(Map<? extends K,? extends V> m)
Constructs a new WeakHashMap with the same mappings as the specified map.

Example of WeakHashMap Class :-

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<String,String>();
Map<String, String> weakHashMap = new WeakHashMap<String,String>();
String hashMapKey = new String("hashMapKey");
String hashMapValue = new String("hashMapValue");
String weakHashMapKey = new String("weakHashMapKey");
String weakHashMapValue = new String("weakHashMapValue");
hashMap.put(hashMapKey, hashMapValue);
weakHashMap.put(weakHashMapKey, weakHashMapValue);
System.out.println("HashMap and WeakHashMap Object Before Garbage Collection --\n");
System.out.println("Hash Map Object : "+hashMap);
System.out.println("Weak HashMap Object : "+weakHashMap);
System.out.println("Hash Map Size : "+hashMap.size());
System.out.println("Weak HashMap Size : "+weakHashMap.size());
System.out.println("\nNow Setting HashMap and WeakHashMap Object Keys to Null --");
hashMapKey = null;
weakHashMapKey = null;
System.out.println("\nNow Calling Garbage Collection --");
System.gc();
System.out.println("\nHashMap and WeakHashMap Object After Garbage Collection --\n");
System.out.println("Hash Map Object : "+hashMap);
System.out.println("Weak HashMap Object : "+weakHashMap);
System.out.println("Hash Map Size : "+hashMap.size());
System.out.println("Weak HashMap Size : "+weakHashMap.size());
}

}

Program Output :-

HashMap and WeakHashMap Object Before Garbage Collection --

Hash Map Object : {hashMapKey=hashMapValue}
Weak HashMap Object : {weakHashMapKey=weakHashMapValue}
Hash Map Size : 1
Weak HashMap Size : 1

Now Setting HashMap and WeakHashMap Object Keys to Null --

Now Calling Garbage Collection --

HashMap and WeakHashMap Object After Garbage Collection --

Hash Map Object : {hashMapKey=hashMapValue}
Weak HashMap Object : {}
Hash Map Size : 1
Weak HashMap Size : 0


No comments:

Post a Comment