Quick revision
The short answer
Record fields are final, but only shallowly: the final field always points to the same List, yet the List itself can still change. The record's hashCode includes the list, so when a caller adds a member, the key's hash changes while it sits in the HashMap, and the map looks in the wrong place. The fix is to copy the list in the compact constructor with members = List.copyOf(members), which makes it unmodifiable and also rejects nulls.
What to remember
In this answer, we saw that record fields are final only shallowly, so a List component can still change, and because the record's hashCode includes the list, a change made while the record is a HashMap key leaves the entry stranded under its old hash. We fixed it with members = List.copyOf(members) in the compact constructor, which copies the elements, makes the stored list unmodifiable and rejects nulls, and we saw that the accessor then safely returns the field directly.
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.
Record fields are final, but only shallowly: the final field always points to the same List, yet the List itself can still change. The record's hashCode includes the list, so when a caller adds a member, the key's hash changes while it sits in the HashMap, and the map looks in the wrong place. The fix is to copy the list in the compact constructor with members = List.copyOf(members), which makes it unmodifiable and also rejects nulls.
Why this question is asked
Records are often described as immutable, and that description is true enough to cause exactly this bug. A Team record goes into a map as a key, and later a lookup with the very same object returns null. Interviewers like the question because it tests three things at once: what final really protects, how HashMap uses hash codes, and how to make a record safe with defensive copies. In this answer, we will recap why final is shallow, reproduce the failed lookup, see why the map cannot find the key, fix it in the compact constructor, and check the accessor side of the problem.
1A Quick Recap: Final Is Shallow
Every record component becomes a private final field. final means the field can be assigned only once, so after construction it always refers to the same object. It says nothing about that object. If the object is a String or an Integer, which cannot change, the record really is unchangeable. If the object is an ArrayList, anyone who holds a reference to that list can add or remove elements, and the record will see the change, because it holds a reference to the same list, not a copy of it.
The record's generated equals and hashCode use every component. For a List component, they call the list's own equals and hashCode, and a list's hashCode is computed from its current elements. So the record's hash code changes whenever the list's contents change.
2Reproducing the Failed Lookup
Here is the situation from the question in a few lines. The caller keeps a reference to the list it passed in, and later modifies it.
Notice the last line. The entry has not been removed; it is stranded. We are asking with the very same object we stored, and the map still cannot find it.
3Why the Map Cannot Find the Key
When we called put, HashMap computed core.hashCode(), chose a bucket from it, and stored the entry there together with that hash. When we call get later, HashMap computes core.hashCode() again. Because the list now has three names, the hash is different, so HashMap usually looks in a different bucket and finds nothing. Even if the new hash happens to land in the same bucket, HashMap compares the stored hash with the new one before calling equals, and they no longer match.
- put(core): hash computed from [ann, bob]entry stored in bucket A with hash h1
- people.add("cat")record's list now [ann, bob, cat]
- get(core): hash computed againnow h2, which points to bucket B
- Entry in bucket B with hash h2?YesReturn the valueNoReturn null
- Entry with h1 still in bucket A
size()is 1 but it cannot be reached
5 steps in order. Play them, or step through them one at a time.
The stored hash is stale; the lookup uses the new one.
So the key is in the map, but under its old hash, and nothing we can do with the current object will reach it. This is why the rule for map keys and set elements is that the fields used by equals and hashCode must not change while the object is inside the collection. The record broke that rule without anyone touching the record directly.
4Fix: Copy the List in the Compact Constructor
The record must stop sharing the caller's list. The cleanest way is a defensive copy, made once, in the compact canonical constructor, the constructor without a parameter list that runs before the compiler assigns the fields. We reassign the parameter, and the compiler stores the copy.
List.copyOf does three useful things at once. It copies the elements, so later changes to the caller's list do not reach the record. It returns an unmodifiable list, so nobody can change the record's list through the accessor either. And it throws a NullPointerException for a null list or a null element, so a Team can never contain a null member. If the argument is already an unmodifiable list created by List.of or List.copyOf, it returns the same instance without copying, so the defence is cheap in the common case.
5The Accessor Side of the Problem
There is a second door we must not forget. A record's accessor returns the field directly unless we override it. Without the copy in the constructor, core.members().add("dan") would change the record just as surely as the caller's reference did.
With List.copyOf in the constructor, the stored list is unmodifiable, so the accessor can safely return it as it is, and we do not need to override members(). Overriding the accessor to return a fresh copy on every call is possible, but it costs an allocation per call and does not fix the shared reference coming in. Copying once on the way in and storing an unmodifiable list fixes both doors.
6Follow-up questions
Would Collections.unmodifiableList(members) be enough?
No. It wraps the caller's list in a read-only view, but the caller still holds the original list and can change it, and the view shows those changes. List.copyOf takes a real copy, so the record is independent of the caller.
Does the same problem happen with arrays as components?
Yes, and it is worse, because arrays are always mutable and a record's equals compares array components by reference, not by contents. Two Team records with equal arrays would not be equal. Prefer List over arrays in records, or copy the array in the constructor and override equals, hashCode and the accessor.
How would you find this bug in production?
The symptom is a map or set whose size stays the same while lookups for known keys return null or false. Logging the key's hashCode at put and at get shows the change. Searching for records with mutable collection components, or adding an ArchUnit or review rule for them, prevents it from coming back.
7Conclusion
In this answer, we saw that record fields are final only shallowly, so a List component can still change, and because the record's hashCode includes the list, a change made while the record is a HashMap key leaves the entry stranded under its old hash. We fixed it with members = List.copyOf(members) in the compact constructor, which copies the elements, makes the stored list unmodifiable and rejects nulls, and we saw that the accessor then safely returns the field directly.
Open with
The record fields are final, but final is shallow. It means the members field always points to the same List object, not that the list cannot change. Here the caller kept a reference to the ArrayList it passed in and added a member later, and the record sees that change because it holds the same list.
Then, if they want more
The record's generated hashCode includes the list, and a list's hashCode is computed from its elements, so the Team's hash code changed while it was a key in the HashMap.
The map had stored the entry under the old hash, so get computes the new hash, looks in a different bucket or fails the stored hash comparison, and returns null, even though size still says one. The fix is a defensive copy in the compact canonical constructor: members = List.copyOf(members).
That copies the elements so the caller's list is no longer shared, it returns an unmodifiable list so nobody can change it through the accessor either, and it throws a NullPointerException for a null list or a null element.
Records return the field directly from the accessor unless we override it, so without the copy, team.members().add would also have broken the key. After the fix, the record is effectively immutable as long as the elements are, which strings are, and it is safe to use as a map key.
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 record Team(String name, List<String> members) is used as a map key. After a caller...": https://scrivio-interview-library.netlify.app/java/records/0218548cab/