Arrays.hashCode() – hash code of an array

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.

Key Points of Arrays.hashCode()

  1. Element-wise Hash Code Calculation:
    • The method computes the hash code based on the contents of the array.
    • For arrays of primitive types, it uses the hash code of each element.
    • For arrays of object types, it uses the hashCode() method of each object.
  2. Array Length:
    • The hash code calculation takes the length of the array into account.
    • Two arrays of different lengths will have different hash codes, even if their contents are similar.
  3. Handling Nulls:
    • If the array is null, Arrays.hashCode() returns 0.

Overloaded Methods

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)

Examples

Hash Code for Primitive Type Arrays

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);
    }
}

Hash Code for Object Type Arrays

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);
    }
}

Important Considerations

  • Consistency with 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.
  • Multidimensional Arrays: For multidimensional arrays, 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.

Hash Code for Multidimensional 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.