We will discuss about hashcode() and equals() methods of Object class and what role they play in an object life cycle. The methods hashCode() and equals() play a distinct role in the objects identity you insert into collections. The specific contract rules of these two methods are best described in the JavaDoc, here I will just describe what role they play, What they are used for, so you know why their implementation is important.
Both equals() and hashcode() are defined in java.lang.Object class and their default implementation is based upon Object information e.g. default equals() method return true, if two objects are exactly same i.e. they are pointing to same memory address, while default implementation of hashcode method return int and implemented as native method.
Equals:
This particular method is used to make equal comparison between two objects. There are two types of comparisons in Java. One is using “= =” operator and another is “equals()”. I hope that you know the difference between this two. More specifically the “.equals()” refers to equivalence relations. So in broad sense you say that two objects are equivalent they satisfy the “equals()” condition. If you look into the source code of Object class you will find the following code for the equals() method.
public boolean equals(Object obj){
return (this == obj);
}
When we say equality, it should adhere by the following properties,
- Reflexive: Always, a = a. In Java, a.equals(a) should always be true.
- Symmetric: If a = b, then b = a. In Java, if a.equals(b) is true, then b.equals(a)should be true.
- Transitive: If a = b and b = c, then a = c. In Java, if a.equals(b) and b.equals(c) is true, then a.equals(c) should be true.
- Consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
Override equals Method
Let us take an example scenario to understand equals() method and study how to override it in our custom implementation. We have a Student class and two instances of Students are equal if they have the same name and id our definition of equality. In our example, though there is an additional property ‘height’, it is excluded from the equals definition to denote that it is entirely our choice.
package com.competency;
public class Student {
private String name;
private String id;
private int height;
@Override
public boolean equals(Object object) {
boolean result = false;
if (object == null || object.getClass() != getClass()) {
result = false;
} else {
Student student = (Student) object;
if (this.name == student.getName()
&& this.id == student.getId()) {
result = true;
}
}
return result;
}
@Override
public int hashCode() {
int hash = 3;
hash = 7 * hash + this.name.hashCode();
hash = 7 * hash + this.id.hashCode();
return hash;
}
public static void main(String args[]) {
Student raj1 = new Student("Raj", "4.11", 4);
Student raj2 = new Student("Raj", "4.11", 5);
Student rohit = new Student("Rohit", "3.12", 4);
System.out.println("Raj1 and Raj2 are equal?: "
+ raj1.equals(raj2));
System.out.println("Raj1 and Rohit are equal?: "
+ raj1.equals(rohit));
System.out.println("Raj1 hashCode: " + raj1.hashCode());
System.out.println("Raj2 hashCode: " + raj2.hashCode());
System.out.println("Rohit hashCode: "
+ rohit.hashCode());
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public Student (String name, String id, int height) {
this.name = name;
this.id = id;
this.height = height;
}
}
Output of code:
Raj1 and Raj2 are equal?: true
Raj1 and Rohit are equal?: false
Raj1 hashCode: 2168458
Raj2 hashCode: 2168458
Rohit hashCode: 555537129
hashcode Method
In the above example did you notice we have overridden another method hashcode() along with equals()? There is an importance to it. We must override hashcode() when we override equals(). Why hashcode should also be overridden when equals is overridden? Because, they both serve the same purpose but in different contexts.
The hashcode() method of objects is used when you insert them into a HashTable, HashMap or HashSet. If you do not know the theory of how a hashtable works internally, you can read about hastables on here
When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object.
The hash code only points to a certain "area" (bucket which is linklist implementation) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The hashtable then iterates this area (all keys with the same hash code) and uses the key's equals() method to find the right key. Once the right key is found, the object stored for that key is returned.
So, as you can see, a combination of the hashcode() and equals() methods are used when storing and when looking up objects in a hashtable.
Here are two rules that are good to know about implementing the hashcode() method in your own classes, if the hashtables in the Java Collections API are to work correctly:
- If object1 and object2 are equal according to their equals() method, they must also have the same hash code.
- If object1 and object2 have the same hash code, they do NOT have to be equal too.
Below is the venn diagram

Interesting Interview Questions:
- When you are writing equals() method, which other method or methods you need to override?
- Can two object which are not equal have same hashCode?
- How does get() method of HashMap works, if two keys has same hashCode?
- Where have you written equals() and hashcode() in your project?
- Suppose your Class has an Id field, should you include in equals()? Why?
- What happens if equals() is not consistent with compareTo() method?
- What happens if you compare an object with null using equals()?
- What is difference in using instanceof and getClass() method for checking type inside equals?
- How do you avoid NullPointerException, while comparing two Strings in Java?
- What is difference between "==" and equals() method in Java?