Why override hashCode(), equals()?

Override hashCode(), equals() for customized comparision instead of comaprision by reference.

Advertisements

We have to override the two methods when we need customized comparision instead of comaprision by reference.

By default both these methods take reference of object for consideration. equals() checks if the objects have the same reference/address (not its content or value) or not and returns true/false accordingly. hashCode() stores the objects in the same bucket or different bucket depending on the hash values by checking the objects address.

But mostly in our applications , we compare the objects based on their values like id or name, etc and not based on their references/memory addresses. So when we compare objects having same values, say , same id then the equals() will return false and hashCode() will return different hash codes(storing objects in different buckets) , considering each value as different, which is not what we expect.

For example : Comparision of two employees having same id

public static void main(String[] args) {

Employee e1 = new Employee(1, “John”);
Employee e2 = new Employee(1, “John”);

HashSet < Employee > EmpHashSet = new HashSet < Employee > ();
EmpHashSet.add(e1);
EmpHashSet.add(e2);
System.out.println(“HashSet size is ” + EmpHashSet.size());
System.out.println(“Checking if hashset contains John : ” + EmpHashSet.contains(new Employee(1, “John”)));

System.out.println(“Comparing the equality between e1 and e2 = ” + e1.equals(e2));
}

Output:

HashSet size is 2
Checking if hashset contains John : false
Comparing the equality between e1 and e2 : false

As we can see the output shows that it is treating the two objects to be having different values as seen in 1st and 3rd line of output and placing them in different buckets as seen in 2nd line of output

To get the correct output by comparing the content of Employee object’s id ,we override equals() by explicitly specifying to compare between the id and hashCode() by returning id, telling it to put objects having equal id in the same bucket

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

@Override
public int hashCode() {
return id;
}

Now, the output will be:

HashSet size is 1
Checking if hashset contains John : true
Comparing the equality between e1 and e2 : true

This overriding can be done to compare objects on names, ages, marks, anything when we dont want the comparision to be done by their addresses, the default way.

Also, follow below rules/contract while overridng these methods:

  1. If two objects are equal, they should have the same hash code.
  2. If two objects have the same hash code, it doesn’t mean that they are equal.
  3. If we override one of the methods then we have to override the other method as well.

 

Hope this helps!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s