This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

A question about hashCode

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I met a SCJP question. It said,

1. Equal objects must have equal hashCodes
2. Unequal objects must have unequal hashCodes
3. Both 1 and 2 are right.

The correct answer is 1. Then, 2 is wrong.
But, if unequal objects may have equal hashCodes, can I still
believe equals method ?

Thanks a lot.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if two objects are not equal according to equal method then there
hashcode may be equal or different.ideally it should be different but it is not requirement.

what you want to know by saying

i am not able to understand please elaborate.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not because objects have the same hashcode that these objects are equal. The hashcode() function is made to find an object quickly, not for testing equality. That's where the equals() method is used for. So although it isn't that good to have several different objects with same hashcode (regarding seeking time) it is perfectly legal.
Objects that are equal are obliged to have same hashcode.

Correct me if I'm wrong...
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Bram

I made a mistake. I was always thinking that the method equals() just
use hashCode() to judge whether two references are sharing the
same object or not.

But, in "Thinking in Java 3rd", the author says "you should always override hashCode( ) when you override equals( ). " Who can tell me the relationship between the two methods?
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should always override both equal and hashcode method if you are using collection that are using hashcode algorithm.
equal method is used to check equality of two objects and hashcode method is used to check hashcode of two objects when they are stored in hashcode bucket
To check object in hashcode bucket there are two steps
1.to check equality of two objects using equal method
2.to check hashcode using hashcode method
When both methods return postive result then object is located in hashcode bucket.If any one of above method will return negative result the object present in hashcode bucket will not be located.
This is why both methods are required to override.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"you should always override hashCode( ) when you override equals( ). "


If you override equals and do not override hashcode, then the contract of equal objects having same hashcode will be broken because the hashcode method of java.lang.Object will be invoked and will give different value for each object
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object API document has detail explains and rules for equals() and hashCode().
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, I draw a conclusion that there is nothing but a contract between equals() and hashCode.

The remaining is how the method equals(Object o) in class Object be implemented, by comparing two objects' memory address?
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Song Gao:
Then, I draw a conclusion that there is nothing but a contract between equals() and hashCode.

The remaining is how the method equals(Object o) in class Object be implemented, by comparing two objects' memory address?



Howdy Song Gao,

I welcome you in Ranch, But unfortunately your name doesn't meet with naming policy.Please change your Screen Name accoding to Naming Policy of JAVA Ranch.

Moderators: Please take care of this Name.

Song---Song(Same as English Word)
Gao----Sing (Gao means Sing in english)

So that means Sing a Song.

Please take care of this issue.
[ October 06, 2006: Message edited by: A D Sharma ]
 
I don't even know how to spell CIA. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic