Arrays.hashCode() is a static method in Java’s java.util.Arrays class that computes a hash code for an array. This method is useful for producing hash codes that can be used in hashing data structures such as HashMap and HashSet.
Arrays.hashCode()hashCode() method of each object.null, Arrays.hashCode() returns 0.Here are the main overloaded variants of the Arrays.hashCode() method:
public static int hashCode(boolean[] a)public static int hashCode(byte[] a)public static int hashCode(char[] a)public static int hashCode(double[] a)public static int hashCode(float[] a)public static int hashCode(int[] a)public static int hashCode(long[] a)public static int hashCode(Object[] a)public static int hashCode(short[] a)import java.util.Arrays;
public class ArrayHashCodeExample {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 3, 4, 5};
int[] array3 = {5, 4, 3, 2, 1};
// Hash code for array1
int hashCode1 = Arrays.hashCode(array1);
System.out.println("Hash code of array1: " + hashCode1);
// Hash code for array2
int hashCode2 = Arrays.hashCode(array2);
System.out.println("Hash code of array2: " + hashCode2);
// Hash code for array3
int hashCode3 = Arrays.hashCode(array3);
System.out.println("Hash code of array3: " + hashCode3);
}
}
import java.util.Arrays;
public class ArrayHashCodeExample {
public static void main(String[] args) {
String[] array1 = {"apple", "banana", "cherry"};
String[] array2 = {"apple", "banana", "cherry"};
String[] array3 = {"apple", "banana", "date"};
// Hash code for array1
int hashCode1 = Arrays.hashCode(array1);
System.out.println("Hash code of array1: " + hashCode1);
// Hash code for array2
int hashCode2 = Arrays.hashCode(array2);
System.out.println("Hash code of array2: " + hashCode2);
// Hash code for array3
int hashCode3 = Arrays.hashCode(array3);
System.out.println("Hash code of array3: " + hashCode3);
}
}
Arrays.equals(): If two arrays are considered equal by Arrays.equals(), they will produce the same hash code when Arrays.hashCode() is called on them. This ensures consistency when using arrays as keys in hash-based collections.Arrays.hashCode() does not perform a deep hash code calculation. Instead, you should use Arrays.deepHashCode() to compute the hash code that considers nested arrays.import java.util.Arrays;
public class ArrayDeepHashCodeExample {
public static void main(String[] args) {
int[][] array1 = {{1, 2, 3}, {4, 5, 6}};
int[][] array2 = {{1, 2, 3}, {4, 5, 6}};
int[][] array3 = {{1, 2, 3}, {6, 5, 4}};
// Deep hash code for array1
int hashCode1 = Arrays.deepHashCode(array1);
System.out.println("Deep hash code of array1: " + hashCode1);
// Deep hash code for array2
int hashCode2 = Arrays.deepHashCode(array2);
System.out.println("Deep hash code of array2: " + hashCode2);
// Deep hash code for array3
int hashCode3 = Arrays.deepHashCode(array3);
System.out.println("Deep hash code of array3: " + hashCode3);
}
}
In summary, Arrays.hashCode() is a useful method for generating hash codes for arrays, ensuring they can be effectively used in hash-based collections. For multidimensional arrays, Arrays.deepHashCode() should be used to properly account for nested arrays.