Why hashCode() must be overriden when equals() is overriden?
posted 10 years ago
Originally posted by Niranjan Prasad: Why hashCode() must be overriden when equals() is overriden?
Because they both go hand-in-hand to determine the unique identity of an object. By definition, every object that is unique has a unique hash code, and every two objects that are equal have the same hash code.
Thus, if you are implementing your own way of testing equality, this consistency must still be maintained, for which you must also implement the hashCode() based on your specific logic for that class, such that the above requirements still hold.