WeakHashMap is a topic which is misterious to many developers. Many find it very tough to understand. Others understand but cannot describe properly.
Those who already understand WeakHashMap may ignore this topic.
I will not try to show how it is implemented. I would like to brief in a question-answer pattern. This will give others a starting point for further reading.
1> What is weakhashmap?
Ans: This is an implementation of Map in java where the keys of the Map are WeakReference Objects.
2> What are WeakReference Objects?
Ans: WeakReference Objects are implemented in the way as follows.
a> They refer to the strongly referenced objects.
b> (i) As soon as the strongly referenced object, i.e. the referent loses all the strong references and (ii) as the very first garbage collector cycle notices that, the WeakReference objects are added to a special referenceque by garbage collector. Also the WeakReference does not refer to the object anymore. This is also made sure by the Garbage Collector.
3> How does the WeakHashMap work?
Ans: In the WeakHashMap the Keys are WeakReference Objects. We call the referent of WeakReference as "StrongKey" and we call the WeakReference objects itself as "WEAKKEY".
a> As the StrongKey loses all the Strong reference the WeakKey also is made to refer "null" and the Weakkey is added to the ReferenceQue by the First Cycle of garbage collector (As stated in the above ans"). Since this point the value referenced by this weakkey is no more accessible from WeakHashMap. I meant to say that the get() method will no more return the value.
b> But the values and the WeakReference objects are still there (although the values unaccessible) in the WeakHashMap occupying memory. Now as soon as any mutator call is invoked to the WeakHashMap (like put() ) the WeakHashMap scans the referenceQue and "removes" all the WeakKeys found in the ReferenceQue from the "WeakHashMap". So now the valus are no more referenced by anything and the values are automatically noticed by the Garbage Collector Cycle.
4> Why is WeakHashMap used?
Ans: In a scenario if we have a vendor provided java class which is made final and we do not have source code we may need this. Say we need to add some special information to each instance (object) of this class. But as the class is not extendable we may create a special value class and use the object of the vendor class as key. But it should be remembered that as soon as the vendor objects are out of scope these values in the Map becomes unnecessary and we need them to be removed automatically. HashMap does not do this. Here WeakHashMap comes to help.
5> What is the difference between softreference and weakreference?
Ans> In the first case the softreference is added to the ReferenceQue after second GarbageCollector cycle as it actually claims the memory of the referent. Whereas in the second case enQueue happens after first cycle.
Courtsey:
http://onjava.com/pub/a/onjava/2001/...on.html?page=1
http://weblogs.java.net/blog/2006/05...eak-references
http://www.ibm.com/developerworks/ja...ry/j-jtp11225/