This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about hashCode

 
Steven Gao Song
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bram Maes
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
joshua antony
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object API document has detail explains and rules for equals() and hashCode().
 
Steven Gao Song
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic