Java hashCode() and equals() methods example


In this tutorial, we show you how to implement the Java hashCode() and equals() methods. These two method helps to check equality of two objects.

hashCode() – It returns the integer value for the object. This hash code is used for determining the bucket location when this object needs to be stored in some Hashtable like data structure.

equals(Object obj) – It checks the equality of two objects. Return true if the object references of two objects verify their equality.

Points to be remember

  1. Both methods belong to Object class so available for every POJO class.
  2. Whenever hashCode() method is invoked on the same object more than once during an execution of a Java program, this method must consistently return the same result. The integer result need not remain consistent from one execution of the program to the next execution of the same program.
  3. If two objects are equal as per the equals() method, then calling the hashCode() method in each of the two objects must return the same integer result. So, If a field is not used in equals(), then it must not be used in hashCode() method.
  4. If two objects are unequal as per the equals() method, each of the two objects can return either two different integer results or same integer results (i.e. if 2 objects have the same hashCode() result does not mean that they are equal, but if two objects are equal then they must return the same hashCode() result).

Let’s see the working example for both cases.

Without overriding hashCode() and equals() method

Create an Employee class that has some fields and its constructor with all fields. Override the toString() methods.

Employee.java
package org.websparrow;

public class Employee {

	private int id;
	private String name;
	private String department;
	private int salary;

	public Employee(int id, String name, String department, int salary) {
		this.id = id;
		this.name = name;
		this.department = department;
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", department=" + department + ", salary=" + salary + "]";
	}
}

Create two objects of Employee class having the same employee details. If you compare the reference and equality of both objects, it will return false for both cases.

Employee emp1 = new Employee(101, "Atul Rai", "IT", 1800);
Employee emp2 = new Employee(101, "Atul Rai", "IT", 1800);

System.out.println(emp1 == emp2); // false
System.out.println(emp1.equals(emp2)); // false

With overriding hashCode() and equals() method

In the same Employee class override the hashCode() and equals() methods.
In my case, I am comparing two employees equality on the basis of their id. You can apply your own logic on basis of any fields.

Employee.java
package org.websparrow;

public class Employee {

	private int id;
	private String name;
	private String department;
	private int salary;

	public Employee(int id, String name, String department, int salary) {
		this.id = id;
		this.name = name;
		this.department = department;
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", department=" + department + ", salary=" + salary + "]";
	}

	@Override
	public int hashCode() {
		final int RAND = 31;
		int result = 1;
		result = RAND * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}

		Employee e = (Employee) obj;
		return (this.id == e.id);
	}
}

Now if you compare both employees, you will get…
false » because == checks the object reference.
true » because equals() check the value of objects and we have the same employee details in both objects.

HashCodeEqualsDemo.java
package org.websparrow;

public class HashCodeEqualsDemo {

	public static void main(String[] args) {

		Employee emp1 = new Employee(101, "Atul Rai", "IT", 1800);
		Employee emp2 = new Employee(101, "Atul Rai", "IT", 1800);

		System.out.println(emp1 == emp2); // false
		System.out.println(emp1.equals(emp2)); // true

	}
}

Generate hashCode() and equals() using Eclipse

If you are using Eclipse IDE, you can generate it effectively. To do that Right click on java file » Source » Generate hashCode() and equals() …

Java hashCode() and equals() methods example

References

  1. hashCode
  2. equals

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.