Consider the following code snippet.
1 public class TestWeakHashMap {
2 public static void main(String[] args) {
3 WeakHashMap map=new WeakHashMap();
4 String s1=new String("java");
5 map.put(s1, "good");
6 String s2=new String("java");
7 map.put(s2,"ok");
//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.
25 comments:
Jobs site
http://www.back2office.com
India's growing job site.Jobs available in sectors like Accounting,
Marketing,IT,Engineering,Finance,BPO/Call Centers,Bio Technology,
Banks,Adminstration,HR/IR/MR,Media,Sales,Security,
Business Mgmt.,Retails,Hotels & Others.
Find Jobs In India,UK,USA,Middle East.
turning whichever retina uksvjksa goodwill hilarious assembly eliminates multiple gases hindley
semelokertes marchimundui
nice post. thanks
Also you may turn your attention on offshore software development company.
You may convert your interest on overseas application growth company.
Java SE WeakHashMap Courses An Observation Training Java Training Institutes in Chennai Java SE Course WeakHashMap has only one entry Training whose key refers Java Training in Chennai Java SE & Java EE Courses wrapper over HashMap and the HashMap's Training Java Online Course WeakHashMap/HashMap does not replace the earlier key Training Java SE Java EE Training
Thanks intended for expressing an exceptionally helpful and intensely educational blog.nimble app
IVR administration is driving ivr organization supplier in USA with all part twilio IVR, for example, laborer after structure and call recording organization. here is association for more data
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
What an awesome post, I just read it from start to end. Learned something new after a long time.
SAP MM training in Chennai
Informative post and very much useful
SAP Training in Chennai | SAP Training Institutes in Chennai | Best SAP Training in Chennai
I have read your blog its very attractive and impressive. I like it your blog.
Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai
Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training
Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai
Core JAVA Training in Chennai | JAVA Training in Chennai
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
Regards,
PMP Training | PMP Training in Chennai
Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.
Java Training in chennai
Java Training institute 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
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
Good discussion.
PMP Training Course in Bangalore
PMP Training Course in Dammam
PMP Training Course in Dubai
PMP Training Course in Jeddah
PMP Training Course in Riyadh
Get trained by an expert who will enrich you with the latest trends.
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Six Sigma Training in Bangalore | Six Sigma Training in Dubai & Get trained by an expert who will enrich you with the latest trends.
Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
Top college in Jaipur|law college|law college in Jaipur|Best law college in Jaipur
Hey Your site is awesome and full of information. I have read you posts they are so informative. Keep Posting wonderful content.
Ani international provide the security solutions for all kind of secruity system and other equipment.
Home security system in jaipur
Wireless Home Security System in jaipur
Realtime attendance machine in jaipur
CCTV Camera dealer in jaipur
Hikvision DVR in jaipur at Rajasthan
security system solutions in jaipur
website design in jaipur
website development company in jaipur
seo company in jaipur
Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
organic oil
organic oil in jaipur
organic cold pressed oils
ayurvedic oil store in jaipur
Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
hospital equipment
sarthak maditech
hospital equipment suppliers
hospital equipment manufacturers
medical equipment manufacturers in jaipur
operation theater lights
hospital suction machine
alcohol breath tester
hospital furniture manufacturer
Wonderful Blog. Keep Posting.
Advanced Excel Training in Chennai
Corporate Excel Training in Mumbai
Best Advanced Excel Training in bangalore
Power BI Training in Chennai
Post a Comment