WeakHashMap in java



In this Collection framework tutorial we will learn what is java.util.WeakHashMap in Collection framework in java.


Contents of page :


1) What is hierarchy of WeakHashMap in java?
-java.lang.Object
-java.util.AbstractMap
 -java.util.WeakHashMap


For more detailed hierarchy information read : Map hierarchy in java


2) java.util.WeakHashMap in java
java.util.WeakHashMap is hash table based implementation of the Map interface, with weak keys.
An entry in a WeakHashMap will be automatically removed by garbage collector when its key is no longer in ordinary use. Mapping for a given key will not prevent the key from being discarded by the garbage collector, (i.e. made finalizable, finalized, and then reclaimed). When a key has been discarded its entry is removed from the map in java.


java.util.WeakHashMap is implementation of the java.util.Map interface in java.


3) The behavior of the java.util.WeakHashMap class depends upon garbage collector
The behavior of the WeakHashMap class depends upon garbage collector in java. Because the garbage collector may discard keys at any time, in WeakHashMap it may look like some unknown thread is silently removing entries. Even if you synchronize WeakHashMap instance and invoke none of its methods,
  • it is possible for the size method to return smaller values over time,
  • for isEmpty method to return false and then true,
  • for containsKey method to return true and later false for a given key,
  • for get method to return a value for a given key but later return null,
  • for put method to return null, and
  • for remove method to return false for a key that previously existed in the WeakHashMap.
Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. Therefore a key will be removed automatically only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.


4) Why to use java.util.WeakHashMap map which is so inconsistent and unpredictable in behaviour?
Let's say we have huge application which consists of lots n lots of object and may run short of memory at any time, we will like garbage collector to quickly discard less used key value pair to free up some memory. As, behavior of the WeakHashMap class depends upon garbage collector.
I believe discarding less used key-value is always going to a better option than running out of memory.

5) Creating java.util.WeakHashMap, put ,get, remove, size, Iterator returned, load factor, complexity of methods in WeakHashMap
Are same as java.util.HashMap in java



6) synchronizing java.util.WeakHashMap in java
We can synchronize weakHashMap by using Collections’s class synchronizedList method.
Map synchronizedMap = Collections.synchronizedMap(weakHashMap);
Now, no 2 threads can access same instance of map concurrently in java.


7) Features of java.util.WeakHashMap
  1. Garbage collector may discard keys at any time in WeakHashMap in java.


  1. WeakHashMap enables us to store data in key-value pair form in java.


  1. WeakHashMap is implementation of the java.util.map interface in java.


  1. Duplicate key- WeakHashMap does not allows to store duplicate keys. If the map already contains a mapping for the key, the old value is replaced in java.


  1. Null elements - One null key can be added in WeakHashMap. And null values are also allowed in WeakHashMap in java.
  2. Insertion order - WeakHashMap does not maintains insertion order in java.
Example in java-
Let’s say we add 3 elements in weakHashMap
weakHashMap.put(1,"ind");    
weakHashMap.put(2,"aus");    
weakHashMap.put(3,"sa");

On displaying insertion order will not be maintained i.e.
3,sa
2,aus
1,ind

  1. synchronized - WeakHashMap is not synchronized (because 2 threads on same WeakHashMap object can access it at same time) in java.

8) When to use java.util.WeakHashMap in java


  1. WeakHashMap must be used when we want garbage collector to discard keys at any time. As, behavior of the WeakHashMap class depends upon garbage collector in java.


  1. WeakHashMap must be used when we want to store data in key-value pair form in java.


  1. WeakHashMap must be used when we want to store only one null key in java.


  1. WeakHashMap must be used when value corresponding to key might be null in java.


  1. WeakHashMap must be used when we don’t care about insertion order in java.


  1. WeakHashMap must be used when we are not working in multithreading environment in java.

So in this Collection framework tutorial we learned what is java.util.WeakHashMap in Collection framework in java.


/** JavaMadeSoEasy.com */



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


ConcurrentHashMap in java - with Segments formation in detail with diagram



EnumMap in java with program



Basic Collection - All properties in tabular form >

Collection - List, Set and Map all properties in tabular form


Collection hierarchy >

Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes




Important Similarity and Differences >
Map Differences >

HashMap and Hashtable - Similarity and Differences


HashMap and ConcurrentHashMap - Similarity and Differences


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences



HashMap vs IdentityHashMap - Similarity and Differences with program



Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >


TreeSet vs ConcurrentSkipListSet - Similarity and Differences with program



TreeMap vs ConcurrentSkipListMap - Similarity and Differences with program




eEdit
Must read for you :