• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap Doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
aslika bahini
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thank you
Saritha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic