File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap Doubt" Watch "HashMap Doubt" New topic
Author

HashMap Doubt

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi There,

I have a doubt in HashMap. In the following I am putting an key & value pair m1.put(saveobj1, 1). I am getting the value by giving the key as m2.get(saveobj1). Also, I tried giving key as m2.get(saveobj2). It gives the same result. I am putting only the saveobj1. But, how am I getting the same result for the key saveobj2?
Can any of you please explain this?


Thanks in Advance
Saritha
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Both saveobj1 and saveobj2 return the same hashCode (so they are in the same bucket), and are equal. So if they are used as a key in a map, they'll refer to the same value.


[My Blog]
All roads lead to JavaRanch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

both saveObj1 and saveObj2 are logically equal.....So retrieving a value which has been set using any of them as the key, will return the same value...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi,

Thanks Christophe and Ankit. It was really helpful for me.

Thank you
Saritha
 
 
subject: HashMap Doubt