In Java, the hashCode() and equals() methods are used to calculate the hash value and check if two objects are equal, respectively. Understand the difference between HashCode() and Equals() methods in Java with the explanation of their usage in calculating hash values and checking object equality. They both return a boolean value, which indicates whether the two objects have the same hash value.
Table of Contents
Method for hashCode in java
The hashCode()method is used to generate a unique integer value for an object, known as its hash code. Its default implementation used to determine the index of the object in the hash table data structure.
General contract associated with hashCode() method?
- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode() method must consistently return the same integer.If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results.
public class MyClass {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + id;
result = 31 * result + (name == null ? 0 : name.hashCode());
return result;
}
public static void main(String[] args) {
MyClass object1 = new MyClass(1, "John Doe");
MyClass object2 = new MyClass(2, "Jane Smith");
System.out.println(object1.hashCode());
System.out.println(object2.hashCode());
}
}
Output

Method for equals() in java
General contract associated with equals() method?
For any non-null reference values x and y, multiple invocations of x.equals(y) should consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
It should return false when passed a null value.
It should be reflexive (x.equals(x) should return true)
It should be symmetric (x.equals(y) should return true if and only if y.equals(x) returns true)
It should be transitive (if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true)
It should be consistent, multiple invocations should return the same result on the same objects.
If equals is overridden, hashcode also should be overridden, so as to maintain consistency between hashcode and equals.
public class MyClass {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyClass other = (MyClass) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static void main(String[] args) {
MyClass object1 = new MyClass(1, "John Doe");
MyClass object2 = new MyClass(1, "John Doe");
MyClass object3 = new MyClass(2, "Jane Smith");
System.out.println(object1.equals(object2));
System.out.println(object1.equals(object3));
}
}
Output

Conclusion
By understanding the differences between these two methods, hashCode() and Equals(), you’ll be able to create code that is both efficient and correct. By following this blog post, you’ll be able to create code that is both reliable and error-free.