Home Java Arrays Methods deepHashCode() Java Arrays.deepHashCode() Method 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.
int[][] grid = { {1, 2}, {3, 4} };
int h = Arrays.deepHashCode(grid); // 32833
Arrays.deepHashCode(new Object[0]); // 1
Arrays.deepHashCode(null); // 0 Call What 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.util.Arrays (since Java 5)
public static int deepHashCode(Object[] a) Parameters Parameter Description a (Object[])The array to hash. May be null; its elements may be arrays, objects, or null.
Return Value Return Value Description intA hash code based on all nested contents; 1 for an empty array and 0 for null.
Important
Like deepEquals, it is not meant for arrays that contain themselves: the documentation leaves the behavior unspecified, and in practice the recursion can end in StackOverflowError. Equal hash codes do not guarantee equal arrays, so hash-based collections still call equals to confirm a match.
Compare with Similar Methods The nested-array family:
Java Examples 1. Hash a grid 2. Use as a set key 3. Mixed contents 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.
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)));
}
} row 0 hash: 994
row 1 hash: 1058
deep hash: 32833
copy same? true How It Works Row hashes first. Row 0 gives 31 * (31 + 1) + 2 = 994, and row 1 gives 31 * (31 + 3) + 4 = 1058.Rows become elements. deepHashCode treats those row hashes as the element hashes: 31 * 1 + 994 = 1025, then 31 * 1025 + 1058 = 32833.Equal copy, equal hash. copy has different row objects but the same numbers, so it also hashes to 32833.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.
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 Matching pair. equals uses deepEquals and hashCode uses deepHashCode, so equal boards always land in the same hash bucket.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.A different board. The third board swaps X and O in the first row, so it is a new entry.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[].
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));
}
} mixed: 123137
empty: 1
null: 0 How It Works Three element hashes. "a" contributes 97, null contributes 0, and the inner {"b"} contributes its own deep hash, 31 * 1 + 98 = 129.Combine in order. 31 + 97 = 128; 31 * 128 + 0 = 3968; 31 * 3968 + 129 = 123137.Empty array. No elements means the starting value, 1, is returned unchanged.Single overload. deepHashCode(null) needs no cast and returns 0 for a null array.Frequently Asked Questions What is the difference between Arrays.hashCode and Arrays.deepHashCode? hashCode uses each element's own hashCode(), so nested arrays contribute identity hashes. deepHashCode hashes nested arrays by their contents.
How do I use a 2D array as a HashMap or HashSet key? 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.
Can deepHashCode take an int[]? No, the parameter is Object[]. Use Arrays.hashCode for a flat primitive array; deepHashCode handles int[] rows inside an int[][].
What does deepHashCode return for null? 0 for a null array; null elements inside the array contribute 0 as well.
Is deepHashCode consistent with deepEquals? Yes. If Arrays.deepEquals(a, b) is true, then Arrays.deepHashCode(a) == Arrays.deepHashCode(b).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion