Java Arrays deepHashCode() Method
Description
The Java Arrays deepHashCode(Object[]) 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).
Declaration
Following is the declaration for java.util.Arrays.deepHashCode() method
public static int deepHashCode(Object[] a)
Parameters
a − This is the array whose deep-content-based hash code to compute.
Return Value
This method returns a deep-content-based hash code for a.
Exception
- NA
Getting Deep Hashcode from an Array of Strings Example
The following example shows the usage of Java Arrays deepHashCode(Object[]) method. In this example, we've created one array of Strings and computed their hashcode using deepHashCode() method and printed
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//initializing an string array
String[] stringArr = { "tuts","point" };
// deepHashCode for String array
int retval = Arrays.deepHashCode(stringArr);
// printing value
System.out.println("The Hash Code of stringArr is:" + retval);
}
}
Output
Let us compile and run the above program, this will produce the following result −
The Hash Code of stringArr is:217575569
Getting Deep Hashcode from an Array of Integers Example
The following example shows the usage of Java Arrays deepHashCode(Object[]) method. In this example, we've created one array of Integers and computed their hashcode using deepHashCode() method and printed
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//initializing an integer array
Integer[] intArr = { 1, 2 };
// deepHashCode for Integer array
int retval = Arrays.deepHashCode(intArr);
// printing value
System.out.println("The Hash Code of intArr is:" + retval);
}
}
Output
Let us compile and run the above program, this will produce the following result −
The Hash Code of intArr is:994
Getting Deep Hashcode from an Array of Objects Example
The following example shows the usage of Java Arrays deepHashCode(Object[]) method. In this example, we've created one array of Student objects and computed their hashcode using deepHashCode() method and printed.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//initializing an Student array
Student[] studentArr = { new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam") };
// deepHashCode for Student array
int retval = Arrays.deepHashCode(studentArr);
// printing value
System.out.println("The Hash Code of studentArr is:" + retval);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
Output
Let us compile and run the above program, this will produce the following result −
The Hash Code of studentArr is:-1497234753