Java traps · Chapter 4 of 16
equals without hashCode
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Java
- In the all labs plan
The first 2 chapters of Java traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Repro.java: an order and its retry, both kept
Write reproduce() in Repro.java. Make a seat order for train 12627, coach B2, seat 34, masala chai, placed at 1792908000000L (6:00 am UTC on 25 October 2026), and its retry: the same train, coach, seat and item, placed 4 seconds later. Give both, in a list, to SeatOrder.dedupe and return the size of the list it gives back.
The check runs your reproduce() twice: beside the original SeatOrder.java it must return 2 (the retry was not dropped), and beside a fixed SeatOrder.java it must return 1. So it must really make two separate orders and call dedupe. No real waiting and no reading input: a retry 4 seconds later is a later placedAt, and reproduce() must finish within 3 seconds.
- Task 2
SeatOrder.java: a retry is dropped
Fix SeatOrder.java so that equal orders have equal hash codes: an order and its retry (the same train, coach, seat and item, placed later) are found as the same order by a HashSet or a HashMap, and dedupe keeps the first of each order, in the order they came, and drops every retry. Orders that differ in train, coach, seat or item are all kept.
Keep equals as it is (placedAt does not matter), the constructor, the getters, toString, and dedupe's log lines: seat delivery: <order> for each order kept, and seat delivery: <n> orders, <m> retries dropped. The check uses its own orders, built at run time, and its retries are always placed later than the order.
- Task 3
SeatOrderTest.java: a test that would have caught it
Write a JUnit test class, SeatOrderTest.java, that would have caught this bug before it reached the stations. The check runs your file three times: beside the original SeatOrder.java, where at least one test must fail; beside the fixed SeatOrder.java, where every test must pass; and beside a wrong fix whose hashCode uses placedAt too, where at least one test must fail.
So test what a real retry looks like: the same order placed a few seconds later is dropped by dedupe (or has the same hash code), and orders for different seats are both kept. No real waiting: each test must finish within 3 seconds.
What you will learn
- Repro.java: an order and its retry, both kept
- SeatOrder.java: a retry is dropped
- SeatOrderTest.java: a test that would have caught it
More in Everyday traps
Build "equals without hashCode" yourself
Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Java traps are free. This one comes with the all labs plan.