Comparing Objects with Equals and Hashcode method

Equals and HashCode method in Java

Equals and HashCode method in Java

We’ll introduce two methods that closely belong together: equals() and hashCode(). We’ll focus on their relationship with each other, how to correctly override them, and why we should override both or neither.

The Object class defines both the equals() and hashCode() methods – which means that these two methods are implicitly defined in every Java class, including the ones we create:

public class Student {

 private int rollno;

 private String name;

  public Student(int rollno, String name) 

  {

    this.rollno = rollno;

    this.name = name;

  }

 

  @Override

  public String toString() {

    return “Student [rollno=” + rollno + “, name=” + name + “]”;

  }

}

 

public class HashMapDemo {

  public static void main(String[] args) {

 

    HashSet<Student> stu = new HashSet<>(); 

    stu.add(new Student(100,”Abdul”));

    stu.add(new Student(101,”Raju”));

    stu.add(new Student(102,”Atish”));

    stu.add(new Student(100,”Abdul”));

    stu.add(new Student(104,”Jenish”));

    for(Student s:stu)

    {

      System.out.println(s);

    } 

  }

}

 

Result : still we found duplicate

Student [rollno=102, name=Atish]

Student [rollno=104, name=Jenish]

Student [rollno=100, name=Abdul]

Student [rollno=101, name=Raju]

Student [rollno=100, name=Abdul]

 

In the above result still we get duplicate, even we use HashSet Mechanism, in that scenario we need to override equals and hashcode method

  1. Equals method what it does is, it will do field to field comparison
  2. Hashcode method what it does is, it will give same hash code to the object whose value in the object are same.

 

public class Student {

private int rollno;

private String name;

public Student(int rollno, String name) 

{

    this.rollno = rollno;

    this.name = name;

}

 

@Override

public int hashCode() {

    final int prime = 31;

    int result = 1;

    result = prime * result + ((name == null) ? 0 : name.hashCode());

    result = prime * result + rollno;

    return result;

}

 

@Override

public boolean equals(Object obj) {

    if (this == obj)

        return true;

    if (obj == null)

        return false;

    if (getClass() != obj.getClass())

        return false;

    Student other = (Student) obj;

    if (name == null) {

        if (other.name != null)

            return false;

    } else if (!name.equals(other.name))

        return false;

    if (rollno != other.rollno)

        return false;

    return true;

}

 

@Override

public String toString() {

    return “Student [rollno=” + rollno + “, name=” + name + “]”;

}

}

 

public class HashMapDemo {

    public static void main(String[] args) {

        HashSet<Student> stu = new HashSet<>(); 

        stu.add(new Student(100,”Abdul”));

        stu.add(new Student(101,”Raju”));

        stu.add(new Student(102,”Atish”));

        stu.add(new Student(100,”Abdul”));

        stu.add(new Student(104,”Jenish”));          

        for(Student s:stu)

        {

            System.out.println(s);

        }   

    }

}

Result – (After overriding the equals and hashcode method now there is no duplicate object present)

Student [rollno=102, name=Atish]

Student [rollno=100, name=Abdul]

Student [rollno=104, name=Jenish]

Student [rollno=101, name=Raju]

Note: As per the Java documentation, both the methods should be overridden to get the complete equality mechanism; using equals() alone is not sufficient. It means, if we override the equals(), we must override the hashcode() method.

Spread the love