Equals and HashCode in Java

Equals checks for values equality of an object. You can override the equals () method to implement the equality conditions based on your need.

Hashcode represents the memory address where your java object lives. You can get this using System.identityHashCode( anObject ).

package examples;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EqualsHashCodeTest {
 public static void main( String[] args ) {
  System.out.println( "Checking Equals operations..." );
  Emp emp1 = new Emp( 1, "John" );
  Emp emp2 = new Emp( 1, "John" );

  if ( emp1.equals( emp2 ) ) {
   System.out.println( "Both are equal" );
  } else {
   System.out.println( "Both are not equal" );
  }
  System.out.println( "\nAdding duplicate employees into list..." );
  List<Emp> emps = new ArrayList<Emp>();
  emps.add( new Emp( 1, "John" ) );
  emps.add( new Emp( 2, "David" ) );
  emps.add( new Emp( 1, "John" ) );
  System.out.println( "List Size:" + emps.size() );

  System.out.println( "\nAdding duplicate employees into set..." );
  Set<Emp> empsSet = new HashSet<Emp>( emps );
  System.out.println( "Set Size:" + empsSet.size() );

 }
}

class Emp {
 private int empId;

 private String empName;

 Emp(int empId, String empName) {
  this.empId = empId;
  this.empName = empName;
 }

 @Override
 public int hashCode() {
  System.out.println( "Inside hashCode()" );
  final int prime = 31;
  int result = 1;
  result = prime * result + empId;
  result = prime * result + ( ( empName == null ) ? 0 : empName.hashCode() );
  System.out.println( "EmpId:" + empId + "\t EmpName:" + empName );
  System.out.println( "HashCode value :" + result );
  return result;
 }

 @Override
 public boolean equals( Object obj ) {
  System.out.println( "Inside equals()" );
  if ( this == obj )
   return true;
  if ( obj == null )
   return false;
  if ( getClass() != obj.getClass() )
   return false;
  Emp other = (Emp) obj;
  System.out.println( "this.empId :" + this.empId + "\t Other.empId :" + other.empId );
  System.out.println( "this.empName :" + this.empName + "\t Other.empName :" + other.empName );
  if ( empId != other.empId )
   return false;
  if ( empName == null ) {
   if ( other.empName != null )
    return false;
  } else if ( !empName.equals( other.empName ) )
   return false;
  return true;
 }

}

Output:

Checking Equals operations...
Inside equals()
this.empId :1 Other.empId :1
this.empName :John Other.empName :John
Both are equal

Adding duplicate employees into list...
List Size:3

Adding duplicate employees into set...
Inside hashCode()
EmpId:1 EmpName:John
HashCode value :2315531
Inside hashCode()
EmpId:2 EmpName:David
HashCode value :65806931
Inside hashCode()
EmpId:1 EmpName:John
HashCode value :2315531
Inside equals()
this.empId :1 Other.empId :1
this.empName :John Other.empName :John
Set Size:2

In the above code,

1. We are comparing two objects using equals() method. Hope it is clear.

2. I am adding list of employees having some duplicate employee details. When i get the size, it gives me 3 , not able to find the duplicates in list. No equals() and hashCode() in Emp class are being invoked.

3. I am adding list of employees in the Set collection. For each Object, it calls the hashcode(), Once the hashcode is equals for two objects, it calls the equals() method to check whether the object is having equal values for all member attributes. Ofcourse, you can change the equality check based on your need. Once it is found as equals, those object will not be added into Set. Hence the size of set is 2.

That’s all. Hope you have got the usage of equals() and hashCode()  now.