aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion on Hashcode and equal method 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 on Hashcode and equal method " Watch "Confusion on Hashcode and equal method " New topic
Author

Confusion on Hashcode and equal method

wonkty wang
Greenhorn

Joined: Jul 17, 2007
Posts: 29
Hi guys,

I thought I understand equal() and hashcode() contract pretty good, but the following exercise from K&B make me confused again.



And the following statments are correct:
1. As the code stands the output will be 3
2. If the hashCode() method is uncommented the output will be 2.

I thought the output should be always 2 regardless the hashcode as the overidden equal method will be the one to determine if the keys are dupublicate or not. In this case, if day are the same then they should be count as the same key. I am confused now, Can anyone please help
[ December 08, 2007: Message edited by: Bear Bibeault ]

SCJP 5.0 <br />-> Change job<br />-> Decide another cert to do
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40


And the following statments are correct:
1. As the code stands the output will be 3
2. If the hashCode() method is uncommented the output will be 2.

I thought the output should be always 2 regardless the hashcode as the overidden equal method will be the one to determine if the keys are dupublicate or not. In this case, if day are the same then they should be count as the same key. I am confused now, Can anyone please help


A hashmap will hash to the bucket first -- and then compares equality with the items in the bucket only. This is why a hashmap has only an order of 1 for most operations -- does not depend on the number of items in the map. Meaning.... if hashed correctly, the should be only a few items in each bucket.

In the second case (uncommented), you are basically putting everything in the same bucket -- hence, the equals() method will be used to test equality for everything. You are also no longer order of one for most operations.

In the first case, you are using the hashcode() method of the Object class, to hash your ToDos object. This method does a really good job at distributing objects, and your three ToDos objects are likely going to three different buckets. And your equals() method is not being used.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by wonkty wang:
...I thought the output should be always 2 regardless the hashcode as the overidden equal method will be the one to determine if the keys are dupublicate or not...

In the case of a HashMap, uniqueness of keys is determined using both the hashCode and the equals method.

Note that according to the hashCode contract, unequal instances might return the same hashCode. A HashMap would store these in the same hashCode "bucket." But as long as the equals method returns false, there is no conflict.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
wonkty wang
Greenhorn

Joined: Jul 17, 2007
Posts: 29
Thanks guys. Your explanation made perfect sense to me now.
wonkty wang
Greenhorn

Joined: Jul 17, 2007
Posts: 29
Hi guys,

I just had second thought on this issue. I end up with the following understanding:

A object equal or not is depends on hashcode + equal() method.
If hashcode return to different location for different object, then equal method wont take effect. Otherwise, if equal()method is overridden, then there should a overwritten on hashcode as well...

For example, String overridden equal()method,then it must overriden hashcode to make equal() method take effect.

Can anyone please let me know if I am correct or not?? I think I am slightly a bit confusing still.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on Hashcode and equal method