Question

Why we should always override hashCode() method whenever we override equals() method? As per my understanding they both seem to fulfill different purpose. hashCode() method is used in hashtables to determine the equality of keys. However equals() method is used to determine the equality of two objects.

Was it helpful?

Solution

If two objects are equal according to the equals method, their hashcodes must be equal as well. Otherwise, lookup in the hash table would fail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top