What is the Contract between equals and hashCode method?
- hashCode()Â method provides the hashcode of an object. Basically, the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. IfÂ you look into the source of Object class, you will find the following code for the hashCode.
- equals()Â method is used to make the equal comparison between two objects. There are two types of comparisons in Java. One is using â€œ= =â€ operator and another is â€œequals()â€. So in the broad sense, you say that two objects are equivalent they satisfy the â€œequals()â€ condition.
- Note that it is generally necessary to override the hashCodeÂ method whenever this method is overridden, so as to maintain the general contract for theÂ hashCodeÂ method, which states that equal objects must have equal hash codes.