HashCode() in Java
Learn via video course

Overview
Hashing is a fundamental Computer Science concept which involves mapping objects or entities to integer values. These values are called hash values or hashcodes of those entities.
The hashCode() method in Java is used to compute hash values of Java objects. The Integer class in Java contains two methods - hashCode() and hashCode(int value) which compute the hash values of Integer objects and primitive int values respectively.
Scope
This article aims to:
- Discuss the concept of Hashing in Java
- Illustrate how to compute hash codes using the hashCode() method
- Explain the hashCode() and hashCode(int value) methods in Integer class
- Discuss how hashCode is employed in Hashing
Introduction
In this article, we will learn one of the most fundamental concepts in Computer Science - Hashing in Java. We implement hashing through a method called hashCode in Java.
Hashing is the process of mapping an object or a primitive data type to some representative integer value using hashing algorithms. In Java, a hash code is an integer value that can be computed for all objects. Efficient hashing algorithms are the base of popular collections framework such as HashMap and HashSet in Java.
Properties of Hashing:
- Objects that are equal based on the output of equals() method must have the same hash value, i.e. they must be mapped to the same integer value.
- Two objects which are not equal may or may not have same hash values. Thus, different objects need not have different hash values.
- The hash values of objects must remain consistent when computed multiple times in the same execution cycle.
- The output of hashing algorithms, when invoked for the same object more than once during the execution of a Java application, must remain the same.
- However, this value need not stay consistent from one execution of the Java application to another.
The Java hashCode() Method
The hashCode() method is defined in Java Object class which computes the hash values of given input objects. It returns an integer whose value represents the hash value of the input object.
The hashCode() method is used to generate the hash values of objects. Using these hash values, these objects are stored in Java collections such as HashMap, HashSet and HashTable.
Example
Through this example, we will test the basic properties of Hashing mentioned in the previous section.
- Two objects with the same value have the same hashcodes.
- Objects with different values usually have different hashcodes.
- Hashcodes of the same object when computed more than once must remain the same.
Code:
Output:
Integer class hashCode Methods in Java
hashCode in Java are of two types based on their respective parameters-
- hashCode() Method - The hashCode() method belongs to the Java Integer class. It returns the hash value of a given Integer object. This method overrides the hashcode() method of the Object class.
- hashCode(int value) Method - This method, which clearly has a parameter, determines the hashcode of its parameter: value.
Declaration of hashCode in Java
The syntax of the two types of hashCode() methods in Java is as follows –
Parameters of the hashCode Method
As we can see, the first type of hashCode method does not have a parameter.
The second type of hashCode method has the following parameter-
Datatype | Parameter | Description |
---|---|---|
int | value | This “value” determines the hashcode. |
Return value of the hashCode Method in Java
The return value of the two types of hashCode method in Java are as follows-
Method | Return Value |
---|---|
hashCode() | Returns the hashcode of the calling Integer object. The hashcode is computed using the primitive int value represented by the Integer object. |
hashCode(int value) | This method returns the hashcode of its primitive int argument i.e. value. |
Implementation of hashCode in Java
Now that we are done with learning about the different types of hashCode() methods in Java and their syntax and parameters, let's implement them in an example.
Example of hashCode(int value)
Code:
Output:
Example of hashCode() method
Now let’s take a look at how to compute the hashCode of Integer objects in Java.
Code:
Use of hashCode in Hashing
For our advanced learners, we can take a look at how hashCode in Java is employed in HashMaps to compute the hash value of the HashMap keys.
- HashMap is a part of collections framework in Java.
- When we store a (key, value) in a HashMap, the hashcode of the key is calculated. The hashcode value received is referred to as a bucket number.
- Keys with the same hash code are present in the same bucket. In the same bucket, multiple (key, value) pairs may be stored in the form of a linked list.
- However keys with different hash codes reside in different buckets.
- When we access the value associated with a particular key, the hashcode of the key is computed again.
- This hashcode value is used to refer to its bucket and the corresponding value is retrieved from it.
Conclusion
- Hashing is a Computer Science concept that maps an object or entity to an integer.
- The hash value of an object is an integer value that is computed using the properties of that object.
- Every object in Java inherits the hashCode() and equals() method.
- Hash codes of two equal objects must be the same. However, the hash codes of two unequal objects need not be distinct.
- An efficient hashing algorithm generates distinct hashcodes for unequal objects.
- The hashCode method should be consistent in its implementation of the equals() function.
- hashCode() and hashCode(int value) methods, defined in the Integer class, compute the hash codes of integer objects or values in Java.