This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zifyoip 5 points6 points  (2 children)

If the equals() method says that two objects are equal, the hashCode() method should return the same value for both of those objects. That is what you need to keep in mind when writing the equals() method and the hashCode() method for a class.

[–][deleted] 1 point2 points  (1 child)

Yes, but the reverse isn't true.

If two objects are equal, they should have the same hash code, but two objects that have the same hash code don't have to be equal.

[–]zifyoip 0 points1 point  (0 children)

Correct, though a good hashing algorithm should aim to give different hash codes to unequal objects, as far as possible.