HudaTutorials.com

java util WeakHashMap Class - WeakHashMap Class in Java

Last updated on

java.util.WeakHashMap Class

WeakHashMap Class in Java

The WeakHashMap functions identically to the HashMap. If the Java memory manager no longer has a strong reference to the object specified as a key, then the entry in the map will be removed. Both null values and the null key are supported.

Is WeakHashMap Thread Safe ?

WeakHashMap is not synchronized.

A synchronized WeakHashMap may be constructed using the Collections.synchronizedMap method.

Is WeakHashMap used for Caching ?

WeakHashMap is not for caching. The idea is, suppose you have a bunch of objects of a certain class that you can't extend, but you want to associate some other piece of information with each object. You can use a Map, with the main object as the key and the extra info as the value. Using a WeakHashMap for this will make sure that your Map won't cause a memory leak, because it won't hold a strong reference to the main (key) object; this will allow the object to be garbage collected when it's no longer needed. When the key is garbage collected, the value will soon be garbage collected too, though not immediately.

java.util.WeakHashMap class Example

/*	Java WeakHashMap class Example
	Save with file name WeakHashMapExample.java	*/
import java.util.WeakHashMap;
import java.util.Enumeration;
public class WeakHashMapExample
{
	public static void main(String args[])
	{
		// java.util.WeakHashMap DECLARATION
		WeakHashMap <String,Integer> h;
		// java.util.WeakHashMap OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		h = new WeakHashMap <String,Integer>();
		// Create a key for the map, keep the strong reference
		String strongReference = new String("ONE");
		// ADD KEY AND VALUE
		h.put(strongReference, new Integer(1));
		h.put("TWO", new Integer(2));
		h.put("THREE", new Integer(3));
		// WeakHashMap OUTPUT BEFORE null the strongReference
		System.out.println(h);
		strongReference = null;
		// JAVA GARBAGE COLLECTION
		System.gc();
		// WeakHashMap OUTPUT AFTER null the strongReference
		// AFTER null THE KEY THE ELEMENT IS REMOVED FROM Map
		System.out.println(h);
	}
}

java.util.WeakHashMap class Example 2

/*	Java WeakHashMap class Example 2
	Save with file name WeakHashMapExample2.java	*/
import java.util.WeakHashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
public class WeakHashMapExample2
{
	public static void main(String args[])
	{
		// java.util.WeakHashMap DECLARATION
		WeakHashMap <String,Integer> h;
		// java.util.WeakHashMap OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		h = new WeakHashMap <String,Integer>();
		//ADD KEY AND VALUE
		h.put("ONE", new Integer(1));
		h.put("TWO", new Integer(2));
		h.put("THREE", new Integer(3));
		// ALLOW null KEY AND VALUE
		h.put(null,null);
		// WeakHashMap KEYS OUTPUT
		Set s = h.keySet();
		Iterator itr = s.iterator();
		int i=1;
		while(itr.hasNext())
		{
			System.out.println("Key " + i++ + " : " + itr.next());
		}
		// WeakHashMap VALUES OUTPUT
		Collection c = h.values();
		Iterator values = c.iterator();
		int j=1;
		while(values.hasNext())
		{
			System.out.println("Value " + j++ + " : " + values.next());
		}
	}
}

java.util.WeakHashMap class Example 3

/*	Java WeakHashMap class Example 3
	Save with file name WeakHashMapExample3.java	*/
import java.util.WeakHashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
public class WeakHashMapExample3
{
	public static void main(String args[])
	{
		// java.util.WeakHashMap DECLARATION
		WeakHashMap <String,Integer> h;
		// java.util.WeakHashMap OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		h = new WeakHashMap <String,Integer>();
		// ADD KEY AND VALUE
		h.put("ONE", new Integer(1));
		h.put("TWO", new Integer(2));
		h.put("THREE", new Integer(3));
		// ALLOW null KEY AND VALUE
		h.put(null,null);
		// WeakHashMap KEYS OUTPUT
		Set s = h.entrySet();
		Iterator itr = s.iterator();
		int i=1;
		while(itr.hasNext())
		{
			// Map.Entry IS INNER INTERFACE OF Map INTERFACE
			Map.Entry entry = (Map.Entry) itr.next();
			System.out.println(entry.getKey()+" "+entry.getValue());
		}
	}
}

java.util.WeakHashMap class Example 4

/*	Java WeakHashMap class Example 4
	Save with file name WeakHashMapExample4.java	*/
import java.util.WeakHashMap;
import java.util.Enumeration;
public class WeakHashMapExample4
{
	public static void main(String args[])
	{
		// java.util.WeakHashMap DECLARATION
		WeakHashMap <String,Integer> h;
		// java.util.WeakHashMap OBJECT CREATION
		// USING DEFAULT CONSTRUCTOR
		h = new WeakHashMap <String,Integer>();
		// ADD AN ELEMENTS
		h.put("ONE", new Integer(1));
		h.put("TWO", new Integer(2));
		h.put("THREE", new Integer(3));
		System.out.println("isEmpty : " + h.isEmpty());
		// RETURNS THE NUMBER OF KEYS IN THIS WeakHashMap
		System.out.println("noof Keys : " + h.size());
		System.out.println("Key ONE value : " + h.get("ONE"));
		System.out.println("Contains key THREE : " + h.containsKey("THREE"));
		System.out.println("Contains value 2 : " + h.containsValue(new Integer(2)));
	}
}