Java Arrays.deepHashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

In Java, Arrays.deepHashCode(a) computes a hash code like Arrays.hashCode, except that when an element is itself an array, it uses that array's contents instead of its identity. It works at any depth.

It is the hash code that goes with Arrays.deepEquals: two arrays that are deeply equal always get the same deep hash code. Use the pair together when a class stores a 2D array and must implement equals and hashCode.

A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.deepHashCode(...); there is no Arrays object to create.

Syntax

Pass an object array; nested arrays are hashed by content.

Java
int[][] grid = { {1, 2}, {3, 4} };
int h = Arrays.deepHashCode(grid);    // 32833
Arrays.deepHashCode(new Object[0]);  // 1
Arrays.deepHashCode(null);           // 0
CallWhat it does
Arrays.deepHashCode(grid)Hash a 2D (or deeper) array by its values.
Arrays.deepHashCode(objs)Mixed elements: objects use hashCode(), arrays are hashed by content.
return Arrays.deepHashCode(cells);Typical line inside hashCode() for a class holding a grid.
Arrays.deepHashCode(null)Returns 0.

For each element the hash is: 0 for null; deepHashCode again for an object array; the matching Arrays.hashCode overload for a primitive array; otherwise the element's own hashCode(). These are combined with the same 31 * result + elementHash rule, starting at 1.

Library declarations
Java
// java.util.Arrays (since Java 5)
public static int deepHashCode(Object[] a)

Parameters

ParameterDescription
a (Object[])The array to hash. May be null; its elements may be arrays, objects, or null.

Return Value

Return ValueDescription
intA hash code based on all nested contents; 1 for an empty array and 0 for null.

Compare with Similar Methods

The nested-array family:

MethodHow it differs
Arrays.hashCode(a)Flat arrays; nested arrays contribute identity hashes.
Arrays.deepEquals(a, b)The equality check that matches deepHashCode.
Arrays.deepToString(a)Readable text for the same nested structure.
grid.hashCode()Identity only; never use it for content comparison.

Java Examples

Example 1 — Deep Hash of a 2D Array, Step by Step

Hash each row on its own, then the whole grid, and compare with an equal copy.

Java
import java.util.Arrays;

public class ArraysDeepHashCodeDemo {
    public static void main(String[] args) {
        int[][] grid = { {1, 2}, {3, 4} };
        int[][] copy = { {1, 2}, {3, 4} };

        System.out.println("row 0 hash: " + Arrays.hashCode(grid[0]));
        System.out.println("row 1 hash: " + Arrays.hashCode(grid[1]));
        System.out.println("deep hash:  " + Arrays.deepHashCode(grid));
        System.out.println("copy same?  " + (Arrays.deepHashCode(grid) == Arrays.deepHashCode(copy)));
    }
}

How It Works

  1. Row hashes first. Row 0 gives 31 * (31 + 1) + 2 = 994, and row 1 gives 31 * (31 + 3) + 4 = 1058.
  2. Rows become elements. deepHashCode treats those row hashes as the element hashes: 31 * 1 + 994 = 1025, then 31 * 1025 + 1058 = 32833.
  3. Equal copy, equal hash. copy has different row objects but the same numbers, so it also hashes to 32833.
  4. What hashCode would do. Arrays.hashCode(grid) would mix in the rows' identity hashes, which differ between grid and copy, so it is the wrong tool here.

Example 2 — Store Game Boards in a HashSet

Wrap a char[][] in a class whose equals and hashCode use the deep methods.

Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ArraysDeepHashCodeKey {
    static final class Board {
        final char[][] cells;
        Board(char[][] cells) { this.cells = cells; }

        @Override
        public boolean equals(Object o) {
            return o instanceof Board && Arrays.deepEquals(cells, ((Board) o).cells);
        }

        @Override
        public int hashCode() {
            return Arrays.deepHashCode(cells);
        }
    }

    public static void main(String[] args) {
        Set<Board> seen = new HashSet<>();
        seen.add(new Board(new char[][] { {'X', 'O'}, {'-', 'X'} }));
        seen.add(new Board(new char[][] { {'X', 'O'}, {'-', 'X'} }));
        seen.add(new Board(new char[][] { {'O', 'X'}, {'-', 'X'} }));
        System.out.println("distinct boards: " + seen.size());
    }
}

How It Works

  1. Matching pair. equals uses deepEquals and hashCode uses deepHashCode, so equal boards always land in the same hash bucket.
  2. A duplicate is ignored. The second board has the same cells as the first, so HashSet.add finds it already present and does not store it again.
  3. A different board. The third board swaps X and O in the first row, so it is a new entry.
  4. Without the overrides. If Board used the default identity methods, all three objects would count as different and the size would be 3.

Example 3 — Strings, null, and an Inner Array

Work out the deep hash of an Object[] that mixes a string, a null, and a nested String[].

Java
import java.util.Arrays;

public class ArraysDeepHashCodeMixed {
    public static void main(String[] args) {
        Object[] data = {"a", null, new String[] {"b"}};
        System.out.println("mixed: " + Arrays.deepHashCode(data));
        System.out.println("empty: " + Arrays.deepHashCode(new Object[0]));
        System.out.println("null:  " + Arrays.deepHashCode(null));
    }
}

How It Works

  1. Three element hashes. "a" contributes 97, null contributes 0, and the inner {"b"} contributes its own deep hash, 31 * 1 + 98 = 129.
  2. Combine in order. 31 + 97 = 128; 31 * 128 + 0 = 3968; 31 * 3968 + 129 = 123137.
  3. Empty array. No elements means the starting value, 1, is returned unchanged.
  4. Single overload. deepHashCode(null) needs no cast and returns 0 for a null array.

Frequently Asked Questions

hashCode uses each element's own hashCode(), so nested arrays contribute identity hashes. deepHashCode hashes nested arrays by their contents.
Wrap it in a class whose equals uses Arrays.deepEquals and whose hashCode uses Arrays.deepHashCode, and do not change the array while it is a key.
No, the parameter is Object[]. Use Arrays.hashCode for a flat primitive array; deepHashCode handles int[] rows inside an int[][].
0 for a null array; null elements inside the array contribute 0 as well.
Yes. If Arrays.deepEquals(a, b) is true, then Arrays.deepHashCode(a) == Arrays.deepHashCode(b).

Next: Arrays.deepToString()

Print nested arrays such as int[][] as readable text.

deepToString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful