How to Override Hashcode Function in Java

Rupam Yadav Feb 02, 2024
  1. Why Do We Override the hashcode() Method
  2. Override hashcode() Method in Java
How to Override Hashcode Function in Java

hashcode in Java is a function that uses the hashing algorithm and returns an integer value representing an object. hashcode() is a part of the Object class which means that this function is available to every class that inherits the Object class.

This article will show how we can override the hashcode() method to provide our implementation.

Why Do We Override the hashcode() Method

Before we proceed to override the hashcode() function, we should understand why we need to override this method. The important thing to have in mind is that the methods equals() and hashcode() go together and it is generally mandatory to override the hashcode() method whenever the equals() function is overridden. It is because that hashcode() says that if the objects are equal, their hash codes must be equal too.

To practically understand the motive behind overriding the hashcode() method, we create an example with two classes called HashCodeExample and DummyClass. In DummyClass, we provide a simple constructor that sets the abc variable. Now in the HashCodeExample class, we create two instances of the DummyClass class and name them as dummyClass1 and dummyclass2 with the same value in their constructors.

We compare the two instances using the equals() method, but the output shows they are not equal.

public class HashCodeExample {
  public static void main(String[] args) {
    DummyClass dummyClass1 = new DummyClass(10);
    DummyClass dummyClass2 = new DummyClass(10);

    System.out.println(dummyClass1.equals(dummyClass2));
  }
}

class DummyClass {
  int abc;

  public DummyClass(int abc) {
    this.abc = abc;
  }
}

Output:

false

It happens because every object instance in Java is given a unique hash code which we can check by calling the hashCode() method on both the objects. The output shows that the integer values of both objects are different.

System.out.println(dummyClass1.hashCode());
System.out.println(dummyClass2.hashCode());

Output:

2065951873
1791741888

To fix the problem of unequal objects, we can override the equals() function and use our implementation. The following code is the same as the first program, but we override the equals() method that takes an Object as an argument and returns a boolean.

In the equals() function, we cast the parameter o of type Object as the type of DummyClass which returns an instance of DummyClass. Now we compare the variable abc of the DummyClass class with the object’s variable abc that is passed in the method as an argument.

The output shows that the result of dummyClass1.equals(dummyClass2) comes as true because we modified the default implementation to return true if the values of the instances are the same.

public class HashCodeExample {
  public static void main(String[] args) {
    DummyClass dummyClass1 = new DummyClass(10);
    DummyClass dummyClass2 = new DummyClass(10);

    System.out.println(dummyClass1.equals(dummyClass2));
  }
}

class DummyClass {
  int abc;

  public DummyClass(int abc) {
    this.abc = abc;
  }

  @Override
  public boolean equals(Object o) {
    DummyClass dummyClassObj = (DummyClass) o;
    return this.abc == dummyClassObj.abc;
  }
}

Output:

true

The above solution only works when we compare the values and not the hash codes because the hash codes of both the objects dummyClass1 and dummyClass2 are still different.

To illustrate it better, we create a HashSet() that returns an object of Set<DummyClass> type and add both DummyClass objects into it using the add() function. Now we print the Set and get two objects with different references in the output, which proves that the DummyClass objects have different hash codes.

This is where we override the hashcode() function to fix the issue, which we will see in the next example below.

import java.util.HashSet;
import java.util.Set;

public class HashCodeExample {
  public static void main(String[] args) {
    DummyClass dummyClass1 = new DummyClass(10);
    DummyClass dummyClass2 = new DummyClass(10);

    Set<DummyClass> dummyClassSet = new HashSet<>();
    dummyClassSet.add(dummyClass1);
    dummyClassSet.add(dummyClass2);
    System.out.println(dummyClassSet);
  }
}

class DummyClass {
  int abc;

  public DummyClass(int abc) {
    this.abc = abc;
  }

  @Override
  public boolean equals(Object o) {
    DummyClass dummyClass = (DummyClass) o;
    return this.abc == dummyClass.abc;
  }
}

Output:

[DummyClass@7b23ec81, DummyClass@6acbcfc0]

Override hashcode() Method in Java

To use our implementation in the hashcode() method, we first override the hashcode() method in the DummyClass class and return the value of the class’s variable abc. Now the hash code is replaced with the value of abc. Now, if we print dummyClassSet, we get only one object because the hash code or the reference is the same.

import java.util.HashSet;
import java.util.Set;

public class HashCodeExample {
  public static void main(String[] args) {
    DummyClass dummyClass1 = new DummyClass(10);
    DummyClass dummyClass2 = new DummyClass(10);

    Set<DummyClass> dummyClassSet = new HashSet<>();
    dummyClassSet.add(dummyClass1);
    dummyClassSet.add(dummyClass2);

    System.out.println(dummyClassSet);
  }
}

class DummyClass {
  int abc;

  public DummyClass(int abc) {
    this.abc = abc;
  }

  @Override
  public boolean equals(Object o) {
    DummyClass dummyClass = (DummyClass) o;
    return this.abc == dummyClass.abc;
  }

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

Output:

[DummyClass@a]

If we print the hash codes of both the objects dummyClass1 and dummyClass2, we get the same hash codes.

System.out.println(dummyClass1.hashCode());
System.out.println(dummyClass2.hashCode());

Output:

10
10
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn