Java Utility Library

Java Objects - hash() Method



The java.util.Objects.hash() method is used to generate a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).

Syntax

public static int hash(Object... values)

Parameters

values Specify the values to be hashed.

Return Value

Returns a hash value of the sequence of input values.

Exception

NA

Example:

In the example below, the java.util.Objects.hash() method returns the hash code for a sequence of input values.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Objects
    Object obj1 = 10;
    Object obj2 = 20;
    Object obj3 = 30;
    Object obj4 = 40;

    //printing the hashcode for the
    //sequence of input values
    int hcode = Objects.hash(obj1, obj2, obj3, obj4);
    System.out.println("The hash code is: " + hcode);
  }
}

The output of the above code will be:

The hash code is: 1241621

❮ Java.util - Objects