473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,382 developers and data experts.

Weak HashMap Summarized

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/
Dec 23 '11 #1
0 3596

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dougjrs | last post by:
I have a HashMap that is storing form data that will later be inserted into a database. I have been able to create the HashMap just fine, but I wanted to be able to take my HashMap and just "dump"...
1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
4
by: David | last post by:
Hi, I have created the following HashMap class. class HashMap: public hash_map<string, string, HashString, HashStringCompare> { public: HashMap(): hash_map<string, string, HashString,...
2
by: xor | last post by:
I'm doing up a school project using java, and am a little new to it (I've worked with other languages for years though). I've seen code posted by the instructor using HashMap like this... ...
11
by: R.A.M. | last post by:
Hello, I have started learning .NET and I have a question: what are, in your opinion, weak sides of .NET 2.0. Thank you very much for your answers. /RAM/
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
4
by: panos100m | last post by:
Hi these are the conents of my hashmap printing out the entrySet. entrySet1: OrderDate=10/30/2007, entrySet2: Level_0={Item_0={ItemTotal= 3.99, ItemName=test® in, ShipDate=10/31/2007,...
15
by: lbrtchx | last post by:
Hi, ~ I have found myself in need of some code resembling a Hashmap ~ This is easily done in Java this way: ~ import java.util.*; // __ public class JMith00Test{
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
yoda
by: yoda | last post by:
So has the title says I need to find a away to loop over a Character ArrayList and replace the characters with a values from a HasMap. This is a simple encryption but I can't figure it out. What...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.