Contract between Hashcode and Equals with Example

 In Uncategorized

A contract between hashcode and equals is a crucial topic in programming. It is essential to understand how hashcode and equals work together to ensure efficient and accurate retrieval of information from data structures.

First, let`s define hashcode. A hashcode is an integer value that represents the state of an object. The hashcode is generated based on the contents of the object. Two objects with identical contents will have the same hashcode. The hashcode is used as a key to store the object in a hash table.

Now, let`s define equals. Equals is a method that compares the contents of two objects. If two objects have the same contents, they are considered equal. The equals method is used to compare objects in data structures like hash tables and sets.

The contract between hashcode and equals is simple. If two objects are equal, they must have the same hashcode. However, the reverse is not true. Two objects with the same hashcode may or may not be equal. This is because different objects can have the same hashcode due to collisions.

Let`s take an example to understand the contract between hashcode and equals.

Consider two objects:


Person person1 = new Person(“John”, 25);

Person person2 = new Person(“John”, 25);


In this example, person1 and person2 have the same attributes (name and age). According to the contract between hashcode and equals, if person1.equals(person2) returns true, then person1.hashCode() must be equal to person2.hashCode().

We can implement the hashCode() and equals() methods in the Person class as follows:


public class Person {

private String name;

private int age;

public Person(String name, int age) { = name;

this.age = age;



public int hashCode() {

int result = 17;

result = 31 * result + name.hashCode();

result = 31 * result + age;

return result;



public boolean equals(Object obj) {

if (obj == null) return false;

if (obj == this) return true;

if (!(obj instanceof Person)) return false;

Person otherPerson = (Person) obj;

return && this.age == otherPerson.age;




In the hashCode() method, we use the 31 x result + attribute formula to calculate the hashcode. This formula is used because it provides a better distribution of hashcodes and reduces the likelihood of collisions.

In the equals() method, we first check if the object being compared is null or the same object. We then check if the object is an instance of Person. If it is, we cast it to a Person object and compare its attributes with that of the current object.

Now, if we call person1.equals(person2), it should return true because both objects have the same attributes. Therefore, person1.hashCode() and person2.hashCode() should also be equal.

In conclusion, understanding the contract between hashcode and equals is crucial in programming. It ensures that objects are stored and retrieved correctly in data structures. It is important to implement these methods correctly to avoid unexpected behavior in your program.