Open In App

equals() and hashCode() methods in Java

Java.lang.object has two very important methods defined: public boolean equals(Object obj) and public int hashCode().

equals() method



In java equals() method is used to compare equality of two Objects. The equality can be compared in two ways:

Syntax :



public boolean equals  (Object obj)

// This method checks if some other Object
// passed to it as an argument is equal to 
// the Object on which it is invoked.

Some principles of equals() method of Object class : If some other object is equal to a given object, then it follows these rules:




// Java program to illustrate 
// how hashCode() and equals() methods work
import java.io.*;
  
class Geek 
{
      
    public String name;
    public int id;
          
    Geek(String name, int id) 
    {
              
        this.name = name;
        this.id = id;
    }
      
    @Override
    public boolean equals(Object obj)
    {
          
    // checking if both the object references are 
    // referring to the same object.
    if(this == obj)
            return true;
          
        // it checks if the argument is of the 
        // type Geek by comparing the classes 
        // of the passed argument and this object.
        // if(!(obj instanceof Geek)) return false; ---> avoid.
        if(obj == null || obj.getClass()!= this.getClass())
            return false;
          
        // type casting of the argument. 
        Geek geek = (Geek) obj;
          
        // comparing the state of argument with 
        // the state of 'this' Object.
        return (geek.name == this.name && geek.id == this.id);
    }
      
    @Override
    public int hashCode()
    {
          
        // We are returning the Geek_id 
        // as a hashcode value.
        // we can also return some 
        // other calculated value or may
        // be memory address of the 
        // Object on which it is invoked. 
        // it depends on how you implement 
        // hashCode() method.
        return this.id;
    }
      
}
  
//Driver code
class GFG
{
      
    public static void main (String[] args)
    {
      
        // creating the Objects of Geek class.
        Geek g1 = new Geek("aa", 1);
        Geek g2 = new Geek("aa", 1);
          
        // comparing above created Objects.
        if(g1.hashCode() == g2.hashCode())
        {
  
            if(g1.equals(g2))
                System.out.println("Both Objects are equal. ");
            else
                System.out.println("Both Objects are not equal. ");
      
        }
        else
        System.out.println("Both Objects are not equal. "); 
    
}

Output:

Both Objects are equal.

In above example see the line:

// if(!(obj instanceof Geek)) return false;--> avoid.-->(a)

We’ve used this line instead of above line:

if(obj == null || obj.getClass()!= this.getClass()) return false; --->(y)

Here, First we are comparing the hashCode on both Objects (i.e. g1 and g2) and if same hashcode is generated by both the Objects that does not mean that they are equal as hashcode can be same for different Objects also, if they have the same id (in this case). So if get the generated hashcode values are equal for both the Objects, after that we compare the both these Objects w.r.t their state for that we override equals(Object) method within the class. And if both Objects have the same state according to the equals(Object) method then they are equal otherwise not. And it would be better w.r.t. performance if different Objects generates different hashcode value.

Reason : Reference obj can also refer to the Object of subclass of Geek. Line (b) ensures that it will return false if passed argument is an Object of subclass of class Geek. But the instanceof operator condition does not return false if it found the passed argument is a subclass of the class Geek. Read InstanceOf operator.

hashCode() method

It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing based collections like HashMap, HashSet, HashTable….etc. This method must be overridden in every class which overrides equals() method.
Syntax :

public int hashCode()

// This method returns the hash code value 
// for the object on which this method is invoked.

The general contract of hashCode is:

Note: Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.

Related link : Overriding equal in Java
Reference: JavaRanch


Article Tags :