• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Confusion on Hashcode and equal method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic