aspose file tools
The moose likes Java in General and the fly likes Confirmation on HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Confirmation on HashMap" Watch "Confirmation on HashMap" New topic
Author

Confirmation on HashMap

Ram Adsumalli
Ranch Hand

Joined: Oct 16, 2006
Posts: 64
Hi Guys

When i trying to haspmap from the online. i got this source code. I got some doubts in this can you guys please explain me the concept


If my explanation is wrong please help me to understand the Concept

a) In the line 1 and Line 2 he created two haspmaps
b) In the Line 3 he is checking that gmap1(HashMap) got the Value for the Object Cigar(key). My question How can he check Because the Map is just created and it is empty.It has no keys. Where he got that object/key from?. In the Line 4 he is putting the Object. How can he check for that key/object before he is putting into the map
c) In the line 4 he is putting the Cigar object and with the value as newHashMap(). So is he creating new hashmap object as a value.If yes what is the use of doing that.
d) In the Last line he is putting that value in the Second Map that is gmap2 so it means the that gmap2 contains null Value .

Am i Confusing with my Java Knowledge. If yes I am sorry, Can any one please explain.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

a) In the line 1 and Line 2 he created two haspmaps


yes.

b) In the Line 3 he is checking that gmap1(HashMap) got the Value for the Object Cigar(key). My question How can he check Because the Map is just created and it is empty.It has no keys. Where he got that object/key from?. In the Line 4 he is putting the Object. How can he check for that key/object before he is putting into the map


Line 3, is checking to see if a value exist with a particular key. You are right, in that the hashmap is empty, so the value that is returned should be null.

You can check for whatever key that you want, and at any time. If that functionality was not allowed, then how would you ever know if the key/value pair exist?

c) In the line 4 he is putting the Cigar object and with the value as newHashMap(). So is he creating new hashmap object as a value.If yes what is the use of doing that.


You can use any object as the value. (You can also use any object as the key, but it would be a good idea to use non-changing objects as keys.)

As for what use of putting a hashmap in a hashmap, now that would depend on your program?

d) In the Last line he is putting that value in the Second Map that is gmap2 so it means the that gmap2 contains null Value


Actually no. In this line, he is changing the reference of gmap2 to refer to the object that is returned, from the gmap1 map, using the "Cigar" key. This should be the hashmap that was created in line 4.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ram Adsumalli
Ranch Hand

Joined: Oct 16, 2006
Posts: 64
Thanks Henry for your explanation. So we can check for the value of the Cigar key After creating the HashMap. There is no need to declare that Cigar is a key for the HashMap before checking the value.

Thanks
Ram
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Confirmation on HashMap
 
Similar Threads
How to access Null key value of an HashMap
Alternate way of inserting the values in map?
query regarding null key in hashmap
Doubt in MapTest example given in K&B
HashMap keys