Tuesday, September 29, 2009

equals(Object obj) and hashCode() methods in Java

Comparing two different objects is one of the mostly achieved tasks for object oriented Java developers. In order to handle this requirement, it is important to understand what makes two different objects in Java equals to each other. For this purpose java.lang.Object class includes equals(Object obj) and hashCode() methods. Main criteria for determining whether different two objects are equal to each other depends actually upon the application's business logic. So it depends on the significance of fields in your Java classes.

Implementation details for the equals(Object obj) method of java.lang.Object class involve deciding about firstly for the types of objects being compared. If two objects are of different types, then the equals(Object obj) method returns false. Else if the compared object can be casted to the current object, it becomes the time for checking the equality of fields in objects.

Another method which is helpful for object comparison in Java is the hashCode() method of java.lang.Object class. hashCode() and equals(Object obj) methods of java.lang.object class are related to each other because if two objects are equal so they are required to generate the same hash code for their common fields. It is suggested to also override hashCode() method of java.lang.Object class whenever you override equals(Object obj) method of java.lang.Object class.

If you are using eclipse for Java development, then ide generates equals(Object obj) and hashCode() methods for you automatically. In order to generate equals(Object obj) and hashCode() methods for your Java classes in eclipse ide, right click in your .java source file and choose from context menu "Source --> Generate hashCode() and equals()" so ide generates hashCode() and equals(Object obj) methods for you.

package util;

public class Person {

 private int id;
 private String name;
 
 public Person(int id,String name){
    this.id = id;
    this.name = name;
 }
 
 @Override
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + id;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
 }
 
 @Override
 public boolean equals(Object obj) {
    if (this == obj)
       return true;
    if (obj == null)
       return false;
    if (!(obj instanceof Person))
       return false;
    Person other = (Person) 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) {
    Person personOne = new Person(1,"Alice");
    Person personTwo = new Person(1,"Alice");
    Person personThree = new Person(1,"Jack");
  
    if(personOne.equals(personTwo)){
      System.out.println("personOne and personTwo are equal!");
    }

    if (personOne.equals(personThree)) {
      System.out.println("personOne and personThree are equal!");
    } else {
      System.out.println("personOne and personThree are not equal!");
    }  
  } 
}

When you run this java class, console output becomes:

personOne and personTwo are equal!
personOne and personThree are not equal!