Java collections Framework- WeakHashMap Class

Java collections Framework- WeakHashMap Class


Posted in : Java Posted on : December 11, 2010 at 6:10 PM Comments : [ 0 ]

A WeakHashMap is a special Map implementation where the keys of the map are stored in a java.lang.ref.WeakReference.

Java collections Framework- WeakHashMap Class

A WeakHashMap is a special Map implementation where the keys of the map are stored in a java.lang.ref.WeakReference.A hashtable-based Map implementation with weak keys. By storing the keys in a weak reference, key-value pairs can dynamically be dropped from the map when the only reference to the key is from the weak reference. This makes the WeakHashMap an excellent implementation for a weakly referenced list, where entries that aren't used elsewhere may be dropped with no side effects. 
Also, just because a key may be dropped, doesn't mean it will immediately be dropped. If the system has sufficient resources, the weak key 
reference that isn't externally referenced could stay around for a long time.
An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.
Like most collection classes, this class is not synchronized. 

The WeakHashMap class supports four constructors.

WeakHashMap( ) - This constructor constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

WeakHashMap(int initialCapacity) - This constructor constructs a new, empty WeakHashMap with the specified initial capacity.

WeakHashMap(int initialCapacity, float loadFactor) - This constructor constructs a new, empty WeakHashMap with the specified initial capacity and load factor.

WeakHashMap(Map m) - This constructor constructs a new WeakHashMap with the same mappings as the specified Map.

The list of methods supported by WeakHashMap Class are shown given below:

put( ) -          Associates the specified value with the specified key in this WeakHashMap. 

clear( ) -       Removes all mappings from the WeakHashMap.

isEmpty( ) -  Returns true if this WeakHashMap contains no key-value mappings. 

remove( ) -   Removes the mapping for this key from this WeakHashMap if it is present. 

keySet( ) -    Returns a set view of the keys contained in this map. 

entrySet( ) - Returns a collection view of the mappings contained in this map. 

values( ) -     Returns a collection view of the values contained in this map. 

size( ) -         Returns the number of key-value mappings in this WeakHashMap.

Here is the  example for demonstration of the WeakHashMap-

Example:- WeakHashMapDemo.java

package devmanuals.com;

import java.util.*;

public class WeakHashMapDemo {
	public static void main(String args[]) {

		WeakHashMap map = new WeakHashMap();
		map.put(1, "A");
		map.put(2, "B");
		map.put(3, "C");
		map.put(4, "D");
		System.out.println("map elements are = " + map);
		map.remove(3);
		System.out.println("map = " + map);
	}
}
Output:-
map elements are = {4=D, 3=C, 2=B, 1=A}

 map = {4=D, 2=B, 1=A}


Download This Code.

Here is the Example of methods implementation of WeakHashMap class.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics