This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode() and equals() method relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode() and equals() method relation" Watch "hashcode() and equals() method relation" New topic
Author

hashcode() and equals() method relation

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
hi
If two objects of an class return same hashcode() are equal
Or when they return true by equals method are they equal.
Or is it necessary that both the above statements be correct for equality of two objects of a class.
mona(varijasmom)


Mona(Varijasmom)
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
hi,
I came across this stmt:
hashCode method is is essential for any class that redefines the equals method. It is required by the general contract for Object.equals. (Equal objects must have equal hash codes.
But this too doesn't give me an explaination regarding my first post on this topic.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Originally posted by varijasmom gupte:
hi
If two objects of an class return same hashcode() are equal
Or when they return true by equals method are they equal.
Or is it necessary that both the above statements be correct for equality of two objects of a class.
mona(varijasmom)

Can you rectify your question a bit?
Mohana Murali
Greenhorn

Joined: Sep 26, 2002
Posts: 27
if two object are equals return by equals method then their hashcodes must be equal. Where as two hashcodes are equal then those object need not be equal. (They may equal or may not equal it does not matter since it's not a required agreement for hashCode or equals methods).
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
hello
It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hashcode value is a 32 bit primitive int, it is not possible to produce a unique hashcode for each value of a primitive long.
Now does this mean that two obj which have unequal hashcode may be equal??? pls resolve this confusion of mine.
krishna madala
Greenhorn

Joined: Nov 18, 2003
Posts: 12
might be but need not be..
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Originally posted by varijasmom:

Now does this mean that two obj which have unequal hashcode may be equal???

If the hashCode method is implemented properly, the answer to your question is NO.
If equals() returns true for two objects, both objects must return the same integer from the hashCode() method. If hashCode values are different, the equals() method should return 'false'. If equals() returns false, the hashCode values can still be the same.
Hope it helps.
Cheers
Harwinder
krishna madala
Greenhorn

Joined: Nov 18, 2003
Posts: 12
sorry for the wrong post, i didnt read properly,,harwinder is absolutely correct, if 2 objects are equal by equals(), then hashode() must consistanly return the same integer for those 2 objects. but if 2 objects have the same hashcode, then those 2 might be equal or need not be by equals()..
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Thanks Harwinder and Krishna.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode() and equals() method relation
 
Similar Threads
Overriding hashCode()
K&B Master Exam Question 56
Reg Hashcodes
HashSet class allows duplicate objects?
Correct implementation of hashcode()