HashCode() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning

In Java, the hashCode() method determines the hash code for objects, pivotal for collections like HashMap and HashSet. The method exists in two variants: the general hashCode() for objects and hashCode(int value) specifically for integers. Proper implementation ensures efficient hashing for optimized collection performance. Java hashCode

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.

  1. Two objects with the same value have the same hashcodes.
  2. Objects with different values usually have different hashcodes.
  3. 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-

  1. 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.
  2. 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-

DatatypeParameterDescription
intvalueThis “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-

MethodReturn 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.