• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Confirmation on HashMap

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic