aspose file tools*
The moose likes Beginning Java and the fly likes Doubt regarding HashMap Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding HashMap Example" Watch "Doubt regarding HashMap Example" New topic
Author

Doubt regarding HashMap Example

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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


~ Mansukh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16
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.


SCJP1.4 , SCWCD 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16
Yes Chris , I meant same . case has been corrected . Thankyou for pointing.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Mar 06, 2001
Posts: 13459

always annotate with @Override and save yourself a heap of sadness.
Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
Can we use indexof() in this example to refer to refer to objects ?
If yes then how?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Jul 20, 2011
Posts: 43
Oh yeah i got you . Thanks for clarification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding HashMap Example