File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes different hashcodes still resulting in equal objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "different hashcodes still resulting in equal objects?" Watch "different hashcodes still resulting in equal objects?" New topic
Author

different hashcodes still resulting in equal objects?

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Please explain the output of below code.

If we have 2 objects of 2 different classes, and I invoke equals on Object1, passing Object2. However, I am returning different hashcodes from the 2 classes.
The comparison in equals method returns true. I want to know how 2 objects with different hashcodes can still be equal?
If so, How am I able to adde the 2 objects to a Set successfully. Shouldn't the 2nd insert return a false??



Are the objects meaningfully equal ?? true
added Emp 1 true
added Emp 2 true
set size is 2
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
per stackOverflow


"If two objects have the same hashcode then they are NOT necessarily equal. Otherwise you will have discovered the perfect hash function. But the opposite is true - if the objects are equal then they have the same hashcode."
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Faisal syed wrote:per stackOverflow


"If two objects have the same hashcode then they are NOT necessarily equal. Otherwise you will have discovered the perfect hash function. But the opposite is true - if the objects are equal then they have the same hashcode."


That is a contract, not a guarantee. You have to write your equals() and hashCode() methods appropriately. In your case, your equals() method is broken because:
1) It returns true when the Objects aren't of the same type
2) It assumes the type of the argument is not the same type as the class with the equals() method
3) The equals() method doesn't work backwards (you will get different results with e2.equals(e1) and e1.equals(e2)
4) It does not fill the contract with hashCode(): Objects can be compared equal when the hashCode() returns different values.

See this article about the contract between equals() and hashCode(): Equals/hashCode Journal Article


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

And why does that matter in the HashSet sample in your original code? The HashSet / HashMap will distribute values into different buckets based on their hashCode() and equality is only compared between objects in the same bucket (they have to have the same hash code). Since your two classes/Objects return different hash code values they will be placed in different buckets and the equality comparison will never be made.
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Many Thanks Steve. I get the the explanation but for the HashSet portion

say if I return same hashcode from the 2 objects, then we have both Employee1 and Employee2
objects in the same bucket. Now the only thing left is the equals comparison which returns true
in case of Employee1 object.
I am still able to add, what I see as duplicate objects.
Please help me in getting the concept right.

This is the op with same hashcodes.


Are the objects meaningfully equal ?? true
added Emp 1 true
added Emp 2 true
set size is 2
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Now, the equals contract is symmetric, and the same hashcode is being returned.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

The result of the equals() will be different depending on if the comparison goes e2.equals(e1) [results in true] or e1.equals(e2) [results in false] which is one of the ways your equals() method is broken. Based on the results, my guess is e1.equals(e2) is being called (but you can't and shouldn't expect the equals to be done in any particular order).

The design and the equals() method are severely flawed. You should not expect different Types [Employee1 and Employee2] to be mutually equal with each other.
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
yes. I perfectly agree. We shouldn't imagine 2 objects of different classes being equal.

However from the latest sample code above
e1.equals(e2) and e2.equals(e1) both return true.

Any clue what is happening here?

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

The hashing method is named hashCode() not hashcode()
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Many thanks for your patience. You helped me finally put the topic to rest
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Great! Glad I could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: different hashcodes still resulting in equal objects?
 
Similar Threads
What is the use of Clone and Equal?
Using java.util.HashSet for Custom Class
why we override hashcode and equal methods
overriding equals() for StringBuilder
Please Explain (Hashcode)