Quick revision
The short answer
Java arrays do not override equals or hashCode, so they inherit identity behaviour from Object: two arrays with the same contents are two different keys, with different hash codes, and the map never finds the second one. The fix is to use a key type with value equality, such as List<String> built with List.of or a record of the individual values. If an array really must be the key, wrap it in a class that uses Arrays.equals and Arrays.hashCode, copy it, and never change it afterwards.
What to remember
In this answer, we saw that a HashMap<String[], Integer> never finds equal-looking keys because arrays keep Object's identity-based equals and hashCode, so two arrays with the same contents are different keys. The fixes are a List built with List.of, a record of the individual values (but not a record holding an array), or, when an array is unavoidable, a wrapper that copies the array and uses Arrays.equals and Arrays.hashCode.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java arrays do not override equals or hashCode, so they inherit identity behaviour from Object: two arrays with the same contents are two different keys, with different hash codes, and the map never finds the second one. The fix is to use a key type with value equality, such as List<String> built with List.of or a record of the individual values. If an array really must be the key, wrap it in a class that uses Arrays.equals and Arrays.hashCode, copy it, and never change it afterwards.
Why this question is asked
This bug looks impossible in a debugger: the map clearly contains a key with "GET" and "/orders", we ask for a key with "GET" and "/orders", and we get null. Interviewers use it because the answer tests whether we understand that a HashMap's notion of "same key" comes entirely from the key's equals and hashCode, and that arrays are a surprising exception to value equality. In this answer, we will reproduce the problem, see exactly why arrays behave this way, and go through three fixes: lists, records and a wrapper class, along with a trap that catches records.
1Reproducing the Problem
Suppose we cache route handlers by method and path, stored as a two-element array.
Notice that the map is not losing data. It holds every entry we put, and if we kept the very same array object we used for put, get would find it. The problem is that a new array with the same contents is, as far as the map is concerned, a completely different key.
2Why Arrays Are Compared by Identity
A HashMap finds a key in two steps. It calls hashCode on the key to choose a bucket, and then it calls equals on the keys in that bucket. Strings, lists, records and boxed numbers override both methods to compare contents. Arrays do not. An array is an object whose class is created by the JVM, and that class inherits equals and hashCode straight from Object, which means identity: an array equals only itself, and its hash code is based on the object, not on the elements.
| Key type | equals compares | Works as a map key? |
|---|---|---|
| String[] | Identity | No: equal contents are different keys |
List<String> (List.of) | Elements in order | Yes |
record RouteKey(String, String) | Each component | Yes |
record Key(String[] parts) | The array by identity | No: same trap inside a record |
Wrapper using Arrays.equals | Elements | Yes, if the array is never changed |
hashCode.So in our map, the second array almost always goes to a different bucket, and even when it happens to land in the same one, equals returns false. The static helpers in java.util.Arrays give the content-based behaviour we expected, but HashMap has no way of knowing it should use them.
3Fix 1: Use a List as the Key
The quickest fix is to use List<String> instead. Every List implementation defines equals and hashCode in terms of its elements and their order, so two lists with the same contents are the same key.
List.of has two useful properties here. It is unmodifiable, so nobody can change a key after it is stored, and it rejects null elements, which surfaces bad input early. If we already have an array, List.of(array) copies it into an unmodifiable list; Arrays.asList(array) only wraps it, so later changes to the array would change the key.
4Fix 2: Use a Record of the Values
When the elements have meanings, such as an HTTP method and a path, a record makes the key clearer than a list and generates content-based equals and hashCode.
But there is a catch, shown in the last line. A record compares each component with that component's own equals, so a record with an array component inherits the array's identity behaviour. A record is a fix only when its components are value types themselves.
5Fix 3: Wrap an Array When It Is Unavoidable
Sometimes the array comes from an API we do not control, and copying into a list for every lookup is too costly. Then we wrap it in a small class that uses the Arrays helpers.
Notice the clone in the constructor. If the wrapper kept a reference to the caller's array and the caller later changed an element, the key's hash code would change while it sat in the map, and the entry would become unreachable. Copying once and never exposing the array keeps the key effectively immutable.
6Follow-up questions
Does the same problem affect int[] or byte[] keys?
Yes, every array type inherits equals and hashCode from Object. For byte[] keys, which are common with hashes and binary identifiers, a wrapper with Arrays.equals and Arrays.hashCode, or a ByteBuffer wrapping a private copy, is the usual fix. ByteBuffer compares remaining contents, but it is mutable, so we must not change its position or data while it is a key.
Why did Java not give arrays content-based equals?
Arrays predate the collections framework and were designed as low-level mutable containers. Content-based equality on a mutable object used as a key is dangerous, and changing the behaviour now would break existing code that relies on identity. The Arrays helper class was added instead, so callers choose explicitly.
What about Arrays.deepEquals?
Arrays.equals compares elements with their own equals, which is not enough for nested arrays such as String[][], because the inner arrays are compared by identity. Arrays.deepEquals and Arrays.deepHashCode recurse into nested arrays.
7Conclusion
In this answer, we saw that a HashMap<String[], Integer> never finds equal-looking keys because arrays keep Object's identity-based equals and hashCode, so two arrays with the same contents are different keys. The fixes are a List built with List.of, a record of the individual values (but not a record holding an array), or, when an array is unavoidable, a wrapper that copies the array and uses Arrays.equals and Arrays.hashCode.
Open with
The map is behaving correctly; the key type is the problem. A HashMap decides whether two keys are the same using the key's hashCode and equals, and arrays do not override either method.
Then, if they want more
They inherit Object's identity semantics, so a new String array with the same contents has a different hash code and is not equal, which means get returns null and a second put creates a second entry. You can see it directly: a.equals(b) is false while Arrays.equals(a, b) is true.
The simplest fix is to use List<String> as the key, for example List.of("GET", "/orders"), because lists define equality by their elements in order, and List.of is also unmodifiable. If the parts have meanings, I would use a record like RouteKey(String method, String path).
One trap there is that a record with an array component has exactly the same problem, because it compares components with their own equals.
If an array key really cannot be avoided, I would wrap it in a small final class that clones the array in its constructor and implements equals and hashCode with Arrays.equals and Arrays.hashCode, and never change the array afterwards.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A HashMap<String[], Integer> never finds keys that look identical. Why?": https://scrivio-interview-library.netlify.app/java/object-contracts/0a8f79685e/