Java traps · Chapter 13 of 16
A stream that never runs its side effect
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Money
- 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.
Why you need this
The third line shows peek running again once a filter is in front of count: now the stream cannot know the size without looking. That is an accident of how this stream is built today, not a promise. peek exists mainly for debugging, and its documentation says it may not be called for elements the stream can skip. Code that only works because of a filter breaks the day someone removes the filter.
A side effect (changing a map, writing a row, sending a message) belongs in a plain for loop, or in forEach, which is a terminal step and runs for every element. Two arriving trains, announced with map and then with forEach (java Board.java):
List<String> arrivals = List.of("12627", "16526");
List<String> announced = new ArrayList<>();
long n = arrivals.stream().map(t -> { announced.add(t); return t; }).count();
System.out.println(n + " trains, announced " + announced);
List<String> announced2 = new ArrayList<>();
arrivals.forEach(t -> announced2.add(t));
System.out.println(arrivals.size() + " trains, announced " + announced2);
2 trains, announced []
2 trains, announced [12627, 16526]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: settled orders that leave the stock as it was
Write reproduce() in Repro.java. Make a stock map that settle can change (a HashMap) with 100 masala chai, settle three orders of masala chai (2, 1 and 3 cups) with Settle.settle, and return how many masala chai the stock has left.
The check runs your reproduce() twice: beside the original Settle.java it must return 100 (six cups settled, and the stock never went down), and beside a fixed Settle.java it must return 94. So it must really settle the orders and read the stock afterwards. No real waiting and no reading input: reproduce() must finish within 3 seconds.
- Task 2
Settle.java: settled orders come out of the stock
Fix settle(Collection<Order> orders, Map<String, Integer> stock) in Settle.java so that every order's quantity is taken out of the stock (item to how many are left) and it returns how many orders it settled: three orders of masala chai of 2, 1 and 3 cups take a stock of 100 to 94 and return 3, and no orders leave the stock as it was and return 0.
The orders may come in a List or a Set. Keep the Order record and the log line, settled <n> orders, as they are. The check uses its own orders and stock, in Lists and Sets, and looks at the stock afterwards, not only at the count.
- Task 3
SettleTest.java: a test that would have caught it
Write a JUnit test class, SettleTest.java, that would have caught this bug before it reached the counters. The check runs your file twice: beside the original Settle.java, where at least one test must fail, and beside the fixed Settle.java, where every test must pass.
So check the stock after settling, not only the number settle returns (that number is right on the bug too): every order's quantity comes out of the stock. No real waiting: each test must finish within 3 seconds.
What you will learn
- Repro.java: settled orders that leave the stock as it was
- Settle.java: settled orders come out of the stock
- SettleTest.java: a test that would have caught it
More in Money
Build "A stream that never runs its side effect" 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.