• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

different hashcodes still resulting in equal objects?

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, the equals contract is symmetric, and the same hashcode is being returned.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashing method is named hashCode() not hashcode()
 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your patience. You helped me finally put the topic to rest
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Glad I could help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic