File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap Doubt" Watch "HashMap Doubt" New topic

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
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

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

Joined: Aug 03, 2008
Posts: 9465

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

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

Thank you
I agree. Here's the link:
subject: HashMap Doubt
It's not a secret anymore!