0

The collection frameworks like HashSet, HashMap, HashTable call hashCode() and equals() in order, to check the two objects are same(For reference, the language I'm learning is java). However if the return values of hashCode() are the same, how could it be possible that the return values of equals() are different? I'm very confused. Does that mean the hash codes of two different objects can be the same?

  • 3
    From [the documentation of of `Object#hashCode()`](https://docs.oracle.com/javase/9/docs/api/java/lang/Object.html#hashCode--): "*It is not required that if two objects are unequal according to the [`equals(java.lang.Object)` method](https://docs.oracle.com/javase/9/docs/api/java/lang/Object.html#equals-java.lang.Object-), then calling the hashCode method on each of the two objects must produce distinct integer results [...]*". Thus, having a `hashCode()` implementation that always returns the same value is fine wrt. the contract. – Turing85 Apr 20 '18 at 12:06
  • It's very likely. It's not the same as unique id. – Kayaman Apr 20 '18 at 12:08

0 Answers0