• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion on Hashcode and equal method

 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Your explanation made perfect sense to me now.
 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic