473,566 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weak HashMap Summarized

18 New Member
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 "WeakHashMa p". 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 GarbageCollecto r 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 3607

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

Similar topics

2
27899
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" it out to the screen to make sure that everything is working as I expect. (It was really to easy to code so I think that I may be mising...
1
16786
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 very ugly and unreadable soon. I'm aware that there exists the for_each-template, but this doesn't make the code any better because the body of the...
4
5753
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, HashStringCompare>() {} };
2
10755
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... public HashMap<Position, Integer> boardNumbers; .... and ... HashMap<Integer, Integer> allNumbers = new HashMap<Integer, Integer>();
11
1590
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
2926
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 greatly appreciated. /** A CarDealer object represents a business that buys and resells used cars */ import java.util.ArrayList; import...
4
1929
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, ItemPrice= 3.99, ItemNumber=8504, ItemQuantity=1}, ShipMethodID=75, ItemCount=1, Items_StatusID=2, Items_Status=Shipped, ShipMethod=,...
15
5655
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
5607
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 key, where the Integer is "1", and String from the value. How could I iterate it? I wrote that, but it doesn't work at the way I expected: ...
0
1676
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 I have to do is get the distinct characters from the text that's been passed in and then assign values to them using a HashMap. I've done that. Also...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.