Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding HashMap Example

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Please look at the code below.



The output we get is :
com.mansukh.test.Dog@190d11
DOG
CAT key
Dog Key
null
5

I understood all of it except the 'null' part. Why didn't we find the 'CatKey' string? Why did it work for the Dog instance as a key. Please explain.

Regards
Mansukhdeep
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, your question is why:

returns null.

You're looking up a value in the map that has a new Cat object as its key. That new Cat object you're using as the key is ofcourse a different new Cat object than the one you use in line 18 to put the value in the map.

Since your class Cat (line 8) does not have equals() and hashCode() methods, it uses the equals() and hashCode() from class Object, which use == and return a unique hash code for every instance. So, if you have to separate Cat objects, they will not be equal.
 
subodh k kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override equals() and hashCode() methods in Cat class because Hashvalue of object should be match with the key passed in map.since new Cat() will create new Object in every call , it does not guarantee the same hash value.so the value for this key is returned as NULL.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
subodh k kumar wrote:You need to override equals() and Hashcode() methods in CAT class because Hashvalue of object should be match with the key passed in map.since new Cat() will create new Object in every call , it does not guarantee the same hash value.so the value for this key is returned as NULL.

You mean the equals() and hashCode() method, as Jesper explained.
 
subodh k kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Chris , I meant same . case has been corrected . Thankyou for pointing.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
subodh k kumar wrote:Yes Chris , I meant same . case has been corrected . Thankyou for pointing.

No problem hashCode may be the most misspelt method, leading to unpleasant surprises.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
always annotate with @Override and save yourself a heap of sadness.
 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use indexof() in this example to refer to refer to objects ?
If yes then how?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catherine austin wrote:Can we use indexof() in this example to refer to refer to objects ?
If yes then how?
I don't think a Map has an indexOf() method. That is only appropriate for Lists. Maps don't store their contents by index. Have a loop at the interfaces in the Java™ Tutorials.
 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah i got you . Thanks for clarification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic