Importance of hashCode() Method in Java
Last Updated :
06 May, 2025
In Java, the hashCode() method tells how objects are stored and compared when we work with collections like HashMap, HashSet, and HashTable. This method returns an integer value, which is known as a hash code, and with the help of this hash code, the program can easily access objects.
In this article, we are going to discuss the importance of hashCode() method in Java.
Prerequisite:
HashMap and HashSet use hashing to manipulate data. They use the hashCode() method to check hash values. The default implementation of hashCode() in the Object class returns distinct integers for different objects. Sometimes, we have to implement hashCode() method in our program.
Java Program to Demonstrates hashCode() and equals() Methods
Example:
Java
// Java program to illustrate use
// of hashcode() and equals() method
import java.util.*;
public class Name {
private final String first, last;
public Name(String first, String last)
{
this.first = first;
this.last = last;
}
public boolean equals(Object o)
{
if (!(o instanceof Name))
return false;
Name n = (Name)o;
return n.first.equals(first) && n.last.equals(last);
}
public static void main(String[] args)
{
Set<Name> s = new HashSet<Name>();
s.add(new Name("Shubham", "Juneja"));
System.out.println(
s.contains(new Name("Shubham", "Juneja")));
}
}
Explanation: In the above example, a Name instance consists of a first name and a last name. Two Name instances are considered equal, as computed by the equals() method, if both their first name and last name are equal. The equals() method compared the first and the last name. Two strings are equal if they consist of the same characters in the same order. Therefore, two Name instances are equal if they represent the same name.
For example:
new Name("Shubham", "Juneja").equals(new Name("Shubham", "Juneja"))
The will return true, because the main method of the program creates two Name instances, both representing Shubham Juneja.
Now, the most important question arises
Why Does the Program Print false?
The program should print true because the two instances represent the same name, but it prints false because the Name instance does not override the hashcode() method. The Name class uses the default hashCode() method two Name instances that are equal by equals() may have different hash codes and this causes the HashSet to not find the second instance, even though they are logically the same. In hash-based collections, the hash code decides which basket an object belongs to. If the hash codes are different, the objects are placed in different basket and not compared for equality and this leads to incorrect result
To fix this, we need to override the hashCode() method so equal objects have the same hash code.
Solution: Override hashCode() Method
To fix the issue, we need to override the hashCode() method to make sure that equal objects produce the same hash value. We can simply do this:
@Override
public int hashCode() {
return 63 * first.hashCode() + last.hashCode();
}
Now, when we run the program, the HashSet correctly identify the two Name instances are equal and then it will print true.
Correct Code:
Java
import java.util.*;
public class Name {
private final String first, last;
// Constructor to initialize first and last names
public Name(String first, String last) {
this.first = first;
this.last = last;
}
// Overriding equals method to compare Name objects
@Override
public boolean equals(Object o) {
if (!(o instanceof Name))
return false;
Name n = (Name) o;
return n.first.equals(first) && n.last.equals(last);
}
// Overriding hashCode method to ensure equal objects have the same hash code
@Override
public int hashCode() {
return 63 * first.hashCode() + last.hashCode();
}
public static void main(String[] args) {
Set<Name> s = new HashSet<Name>();
s.add(new Name("Shubham", "Juneja"));
// Output will now be true
System.out.println(s.contains(new Name("Shubham", "Juneja")));
}
}
Similar Reads
Importance of Hashcode method in Java In Java, the hashCode() method tells how objects are stored and compared when we work with collections like HashMap, HashSet, and HashTable. This method returns an integer value, which is known as a hash code, and with the help of this hash code, the program can easily access objects.In this article
3 min read
Integer hashCode() Method in Java The java.lang.Integer.hashCode() method of Integer class in Java is used to return the hash code for a particular Integer . Syntax: public int hashCode() Parameters : The method does not take any parameters. Return Value: The method returns a hash code integer value for this object, which is equal t
2 min read
Set hashCode() Method in Java In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe
2 min read
Vector hashCode() Method in Java The java.util.vector.hashCode() method in Java is used to get the hashcode value of this vector. Syntax: Vector.hashCode() Parameters: The method does not take any parameter. Return Value: The method returns hash code value of this Vector which is of Integer type. Below programs illustrate the Java.
2 min read
IdentityHashMap hashCode() Method in Java The java.util.IdentityHashMap.hashCode() method in Java is used to fetch the hash code value of a particular this IdentityHashMap. A map consists of a number of buckets to store the key-value pair. Each bucket has a unique identity and when a key-value pair is inserted into a bucket, the key's hashc
2 min read
YearMonth hashCode() method in Java The hashCode() method of YearMonth class in Java is used to create a suitable hash code for this YearMonth instance with which it is used. Syntax: public int hashCode() Parameter: This method does not accepts any parameter. Return Value: It returns a suitable integral hash code value for this YearMo
1 min read