Quick revision
The short answer
@Data generates hashCode from all fields, including status, so calling setStatus(SHIPPED) changes the order's hash code. The HashSet filed the order in the bucket for its old hash, and contains() now looks in the bucket for the new hash, finds nothing and returns false. The fix is to base equals and hashCode on something that never changes, such as the id with @EqualsAndHashCode(onlyExplicitlyIncluded = true), or to make the object immutable with @Value.
What to remember
In this answer, we saw that @Data computes hashCode from all fields, so changing status changes the hash, and the HashSet then looks in a different bucket from the one where it stored the order. contains and remove fail and add creates a duplicate. We fixed it by limiting equals and hashCode to the stable id with onlyExplicitlyIncluded and @EqualsAndHashCode.Include, or by making the class immutable with @Value, and we added a unit test that checks the equality behaviour.
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.
@Data generates hashCode from all fields, including status, so calling setStatus(SHIPPED) changes the order's hash code. The HashSet filed the order in the bucket for its old hash, and contains() now looks in the bucket for the new hash, finds nothing and returns false. The fix is to base equals and hashCode on something that never changes, such as the id with @EqualsAndHashCode(onlyExplicitlyIncluded = true), or to make the object immutable with @Value.
Why this question is asked
This bug is confusing the first time we meet it: the object is clearly in the set, we can even see it when we iterate, and yet contains() says no. Interviewers ask it because it tests whether we understand how hash-based collections work and what @Data quietly decides for us. In this answer, we will recap how HashSet finds an element, reproduce the problem, see exactly why the lookup misses, and then look at the fixes and a test that keeps the bug from coming back.
1A Quick Recap of How HashSet Finds an Element
A HashSet is backed by a HashMap. When we add an object, the set calls its hashCode(), uses that number to choose a bucket, a slot in an internal array, and stores the object there. When we later call contains(), the set calls hashCode() again, goes straight to the bucket for that number, and compares the objects in that bucket with equals(). It never searches the other buckets; that is what makes it fast.
So a HashSet relies on one promise: while an object is in the set, its hashCode() must not change. The documentation of Set warns that the behaviour is not specified if an element is changed in a way that affects equals while it is in the set.
2Reproducing the Problem
Here is the class from the question and the steps that break it.
Notice how bad it gets. contains() fails, remove() silently does nothing, and add() puts the same object into the set a second time. Code that tracks open orders in a set would now leak orders and process some twice.
3Why the Lookup Misses
@Data includes @EqualsAndHashCode, which by default computes hashCode from every non-static, non-transient field, here id and status. With status NEW the hash might be 1000, which lands in bucket 8; with status SHIPPED it becomes, say, 1203, which lands in bucket 3. The object itself did not move: it is still in bucket 8. contains() calls the new hashCode(), looks only in bucket 3, finds it empty and returns false.
| Moment | hashCode() | Bucket used | Result |
|---|---|---|---|
after add(), status NEW | 1000 (example) | 8, object stored here | contains() -> true |
after setStatus(SHIPPED) | 1203 (example) | 3, empty | contains() -> false |
remove(order) | 1203 | 3, empty | nothing removed |
add(order) again | 1203 | 3, stored again | size() -> 2 |
So the object is not lost, it is misfiled. The set's index still says bucket 8, but the object now claims to belong to bucket 3. The same happens with HashMap keys, and also with entities and caches that are keyed by such objects.
4Fix 1: Base Equality on a Stable Identity
An order has a natural identity, its id, which never changes. Equality and the hash should use only that. Lombok lets us say so explicitly.
Now hashCode() depends only on id, so changing the status does not move the object. We replaced @Data with the individual annotations, which also makes the equality rule visible to every reader. Note that two Order objects with the same id now compare equal even if their status differs, which is usually what we mean by "the same order".
5Fix 2: Make the Object Immutable
The other option is to make changes impossible. With @Value the fields are final and there are no setters, so the hash code can never change. To "change" the status we create a new object, and we must remove the old one from the set and add the new one.
This is the right model when the object is a value, a snapshot of data. When it is an entity with an identity that lives over time, Fix 1 fits better. Either way, the rule is the same: mutable objects used as set members or map keys must not include mutable fields in equals and hashCode.
6A Test That Keeps It Fixed
Equality rules are easy to break later, for example when someone adds @Data back. A short unit test pins the behaviour.
7Follow-up questions
Would a TreeSet have the same problem?
Yes, in its own way. A TreeSet places elements by compareTo or a Comparator. If the comparison uses status and status changes, the element sits in the wrong place in the tree and lookups miss it, exactly like the wrong bucket in a HashSet.
Why does iterating still find the object?
Iteration walks every bucket and returns what is stored there, without using hashCode(). Only lookups by hash, contains, remove and add, go to one bucket. That is why the object is visible but not findable.
Is it ever fine to use @Data on objects in a set?
Only if the objects are never changed while they are in the set. That is a fragile promise, so it is better to make the rule structural: use @Value for values, or id-based equality for entities.
8Conclusion
In this answer, we saw that @Data computes hashCode from all fields, so changing status changes the hash, and the HashSet then looks in a different bucket from the one where it stored the order. contains and remove fail and add creates a duplicate. We fixed it by limiting equals and hashCode to the stable id with onlyExplicitlyIncluded and @EqualsAndHashCode.Include, or by making the class immutable with @Value, and we added a unit test that checks the equality behaviour.
Open with
The problem is that @Data generates equals and hashCode over all fields, so status is part of the hash. When I add the order, HashSet calls hashCode, picks a bucket from it and stores the order there.
Then, if they want more
When I call setStatus(SHIPPED), the hash code changes, but the order stays in the old bucket. contains calls hashCode again, goes to the bucket for the new value, finds nothing and returns false. It gets worse: remove does nothing and add puts the same object in a second time, while iteration still shows it.
The rule is that anything used as a set member or map key must not have equality based on mutable fields. For an order, the identity is the id, so I would drop @Data, use @Getter and @Setter, and put @EqualsAndHashCode(onlyExplicitlyIncluded = true) on the class with @EqualsAndHashCode.Include on the id. Then changing the status does not move it.
If the order is really a value, I would make it immutable with @Value and @With, and replace it in the set when the status changes. I would also add a unit test that changes the status and checks that the set still contains the order, so nobody quietly brings @Data back.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 class Order is annotated @Data and stored in a HashSet. After calling...": https://scrivio-interview-library.netlify.app/java/lombok/7f9ee04d19/