//Since s1.equals(s2) is true and hash is same, the earlier value //against key s1 ("good") in the map is replaced by the new one. ("ok")

8 s1=null;

9 System.gc(); //Verify Full GC with the -verbose:gc option

10 System.out.println(map.size());11 }12 }

What do we expect the output to be? 1? No, Not exactly.

Here s1 and s2 are two different objects on the heap. So in line 5, a new (key,value) pair with key s1 is put into the map. Later when a (key,value) with key s2 is being put into the map, it checks for equals on s1 and s2 and their hashcode. When it finds the equals returns true and hashcde is same, it replaces the value of the earlier entry with the new value. But the issue(?) here is, WeakHashMap/HashMap does not replace the earlier key while adding a (key, value) pair whose key is actually a duplicate key in the map.So even after putting an entry with key s2, the WeakHashMap has only one entry whose key refers to the object refered by s1 and not s2.Now the object on the heap refered by s1, has one strong reference(through s1) and one weak reference through the WeakHashMap.Later when I say s1=null, the object on the heap refered to by s1 lost the strong reference and when gc happens, the entry is removed from the map.

So thats how it works.

Also note WeakHashMap is only a wrapper over HashMap and the HashMap's put api says " If the map previously contained a mapping for this key, the old value is replaced by the specified value."

So just be careful when you use WeakHashMap and your usage scenario is similar to the above.

This blog provides a observation of week hash map. This helps to learn more about this process. This gives the coded explanation. That is very helpful to me. Thanks for this blog.Java Training in Chennai

I am apparently an innocent abroad, but it never occurred to me that my blogs were aces of anyone's apprehension to bother hacking wordpress backup them. However, hackers assume to appearance alike the atomic of blogs and users as fair game. Learn what I accept done to aegis my Wordpress blogs in future, and accomplish it simple to balance with no abstracts loss.

Admonished is forearmed! So it is with payday advances. In the event that you realize what this sort of loan will cost you, you will be in a superior position to measure the advantages and disadvantages of settling on it. Payday Loans Chicago