hashCode method in Java

1. Introduction

In this tutorial, we’ll discuss what is hash code in Java and the hashCode() method provided in Object class. Hash code is one of the most important concepts of Java and used extensively in data structure implementations like HashMap. Whenever you want to override the custom behavior of few data structures provided by Java, understanding of hash code is must. For example, if you want to use objects of your class as key in HashMap, knowledge of hashCode() is must.

The hashCode() method is usually read in connection with equals() method. The reason is there is a general contract between equals() method and hashCode() method. This contract must be followed. In this tutorial, we’ll discuss the contract between hashCode() and equals() method as well.

2. What is hash code in Java?

In Java, a hash code is an integer value that is linked with each object. hashCode() returns an integer value, generated by a hashing algorithm. The hash code in Java is not the memory address of the object. This is also good from security point of view as the memory address of the object is not directly accessible.

According to the Java documentation:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

3. hashCode() method in Java

This method returns the hash code value for the object. This method is provided in Object class. hashCode() method is provided in Object class and is inherited in every object from the parent class.

4. Example of hashCode()

package com.learnitweb;

import java.util.ArrayList;
import java.util.List;

public class HashCodeExample extends Thread {

	public static void main(String args[]) throws InterruptedException {
		String str = new String("hello");
		List list = new ArrayList();

		System.out.println("hash code of str: " + str.hashCode());
		System.out.println("hash code of list: " + list.hashCode());


hash code of str: 99162322
hash code of list: 1

5. General contract of hashCode() and equals()

  • Whenever hashCode() method is invoked on the same object during the execution of Java application, it must return the same value, provided the no information used in comparison of objects in equals() method is changed.
  • If the two objects are equal according to the equals() method, then the hash code returned by the hashCode() for both the objects will always be the same.
  • It is not mandatory that if two objects have same hash code then the two objects are equal according to the equals().

6. Override hashCode() method

We can override the hashCode() method but we must take care that it follows the general contract. The most important point to note is that the hashCode() implementation should return different hash code values for unequal objects. However, it is perfectly legal to return a fixed value whenever hashCode() is invoked.

Many IDEs provide the feature to generate hashCode() implementation. Following is an example of hashCode() generated by the Spring Tool Suite:

package com.learnitweb;

public class Student {
	long id;
	String name;

	public long getId() {
		return id;

	public void setId(long id) {
		this.id = id;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;