Java Arrays - deepHashCode() Method
The java.util.Arrays.deepHashCode() method returns a hash code based on the "deep contents" of the specified array. For any two arrays a and b such that Arrays.deepEquals(a, b), it is also the case that Arrays.deepHashCode(a) == Arrays.deepHashCode(b).
Syntax
public static int deepHashCode(Object[] a)
Parameters
a |
Specify the array whose deep-content-based hash code to compute. |
Return Value
Returns a deep-content-based hash code for the array.
Exception
NA.
Example:
In the example below, the java.util.Arrays.deepHashCode() method is used to check whether the two arrays are equal or not.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating three array objects Object Arr1[] = {10, 5, 25}; Object Arr2[] = {10, 5, 25}; Object Arr3[] = {10, 20, 30}; //print the deep hash code of arrays System.out.println("deepHashCode of Arr1: "+ Arrays.deepHashCode(Arr1)); System.out.println("deepHashCode of Arr2: "+ Arrays.deepHashCode(Arr2)); System.out.println("deepHashCode of Arr3: "+ Arrays.deepHashCode(Arr3)); //check Arr1 and Arr2 for equality boolean check = (Arrays.deepHashCode(Arr1) == Arrays.deepHashCode(Arr2)); System.out.println("\nAre Arr1 and Arr2 equal?: "+ check); //check Arr1 and Arr3 for equality check = (Arrays.deepHashCode(Arr1) == Arrays.deepHashCode(Arr3)); System.out.println("Are Arr1 and Arr3 equal?: "+ check); } }
The output of the above code will be:
deepHashCode of Arr1: 39581 deepHashCode of Arr2: 39581 deepHashCode of Arr3: 40051 Are Arr1 and Arr2 equal?: true Are Arr1 and Arr3 equal?: false
❮ Java.util - Arrays