HashCode() in Java | Java hashcode Method – Scaler Topics

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.

Java Hashcode Method

Properties of Hashing:

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

  2. Two objects which are not equal may or may not have same hash values. Thus, different objects need not have different hash values.
  3. The hash values of objects must remain consistent when computed multiple times in the same execution cycle.
  4. 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.
  5. However, this value need not stay consistent from one execution of the Java application to another.

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:

import

java.io.*;

public

class

CheckProperties

{

public

static

void

main

(String args[])

{

String a =

"100"

;

String b =

"100"

;

// Printing the hashcodes of a and b

System.out.println(

"HashCode of a = "

+ a +

": "

+ a.hashCode());

System.out.println(

"HashCode of b = "

+ b +

": "

+ b.hashCode());

// Declaring a different variable

String c =

"500"

;

// Printing the hashcode of c

System.out.println(

"HashCode of c = "

+ c +

": "

+ c.hashCode());

// Second Computation of a's hashcode

System.out.println(

"HashCode of a = "

+ a +

": "

+ a.hashCode());

} }

Output:

HashCode of a =

100

:

48625

HashCode of b =

100

:

48625

HashCode of c =

500

:

52469

HashCode of a =

100

:

48625

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 –

public

int

hashCode

()

public

static

int

hashCode

(

int

value)

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-

DatatypeParameterDescriptionintvalueThis “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 ValuehashCode()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:

// Example of hashCode(int value)

public

class

Main

{

public

static

void

main

(String[] args)

{

int

value =

144

;

System.out.println(

"HashCode of value = "

+ value +

": "

+ Integer.hashCode(value)

); } }

Output:

HashCode of value =

144

:

144

Example of hashCode() method

Now let’s take a look at how to compute the hashCode of Integer objects in Java.

Code:

public

class

IntegerObjectHashCode

{

public

static

void

main

(String[] args)

{

// Create integer object

Integer val =

new

Integer(

"189"

);

// Compute the hashCode of this integer object

int

hashValue = val.hashCode();

System.out.println(

"HashCode of val: "

+ hashValue);

} }

HashCode of val:

189

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.