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 confusion in HashMaps 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 "confusion in HashMaps " Watch "confusion in HashMaps " New topic
Author

confusion in HashMaps

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Hello friends,
This code is from Kathy Sierra book SCJP6 page585. I tried this code and it is also working, but the application of the behavior of HashMaps, (which says that, "if the key used to generate hash code is altered then the object cannot be picked back from the Map") in this program is puzzling me big time.



which produces the below given output


D:\Education\Java\JavaStudyRoom>javac MapTest.java

D:\Education\Java\JavaStudyRoom>java MapTest
Dog@4
DOG
CAT key
Dog key
null
5
null
Dog key
null


According to my understanding here we are in #1, trying to extract value on basis of dog object whose name is "magnolia". Since the key is generated using dog object with "clover" as name, therefore the hashcodes mismatches and we get null.
In 2nd case we do the same but with clover as name of dog object, but this time the hashcodes matches and equals() also satisfies, and we get the dog key.
But what is happening in Case 3,confuses me:
here though in #3 we assign arthur to d1'sname instance variable but in #4 we are passing a dog object with clover as name.
So ideally the hashcodes should match + equals should return true. So the output should have been dog key like in case 2.
But I dont understand why is this not happening and why we get null???

I am sure that this is the correct behavior and output, but then can you tell me where exactly I am getting wrong with my logic???

Thanks in advance,

BR
Suvojit
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17


All the extra code which was not related to your problem was making it difficult to understand your problem so I removed it. Now at line 4, you are getting null because the when you call m.get(new Dog("clover")), the hashCode will match but the equals method will not return true as d1.name is "arthur" and we are passing it a dog with the name "clover"...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Also if you search the forum, there have been discussions in the past about this question like here, here, here, here ......... See if you get your answer there...
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Ankit Garg wrote:Also if you search the forum, there have been discussions in the past about this question like here, here, here, here ......... See if you get your answer there...


The first link cleared my doubt and its now clear to me now....
In few days I will write my interpretation of this concept( i dont have time to write it now as I have other studies to do), please verify it then.

Thank you so much

Suvojit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion in HashMaps