Equals() & HashCode() in Java

Spread the love

Equals(): The equals() method is used to compare two or more objects for equality. It is part of the java.lang.Object class but we can override the same in child class as well according to our requirement as all classes by default extend java.lang.Object class.

As per the method definition in object class, equality is checked on the basis of the object references i.e; Object obj1 is equal to Object obj2 if obj1 == obj2. Both objects should have same reference.

It is declared as below.

public boolean equals(Object obj) {

return (this == obj);

}

HashCode(): It is an integer representation of an object or you can say integer hash code value of the object. It is part of the java.lang.Object class but we can override the same in child class as well according to our requirement as all classes by default extend java.lang.Object class.

If 2 objects are equal that is if the obj1 == obj2, then hash code of both the objects would be same. But there are cases where hashCode for different objects are also same or you can say have the same hash code value. Hence hash code only can’t be used for equality check for objects.

So some important points which we make out now are :

  • If two objects are equal according to the equals method, then their hash code values should be equal too or you can say their hashcodes must be equal too.
  • If two objects are not equal according to the equals method, then their hashes can be equal or not equal.

If the object is altered i.e; a property value is changed then the hash code value for that object also changes.

How to declare an hashCode method?

public int hashCode(){

}

Overriding Equals() & HashCode():

Sometimes there may be a case in which there are 2 objects of the same class and class variables of both objects have equal values. So ideally they should be equal? But they are not since as we saw above, equals() method checks equality on the basis of memory reference (==).

If there comes a need to change the logic for equality check for a particular class then we would need to override the equals method in the child class.

But remember one important thing!!!

If we override equals(), then we would need to override hahsCode() as well because for equal objects, the hash code values should also be equal.

Class Employee
{
public String emp_name;
public String emp_email;

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;

	result = prime * result + (((emp_name == null) ? 0 : emp_name.hashCode()) + ((emp_email == null) ? 0 : emp_email.hashCode()));
	return result;
}

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

In the above example, Employee class has overriden equals and hashCode methods.

In equals, we are comparing the member variables which are emp_name and emp_email and returning equals as true or false.

Similarly hashCode is also over ridden to return same hash code value for equal objects.