Blog
Java Stream Side Effects: Clean Syntax, Dirty Semantics
Streams are good at transforming values. They become unpleasant when every stage updates a list, increments a counter, writes a database row, and depends on encounter order.
The code still looks like a pipeline. The semantics are now a command script wearing fluent syntax.
The Mutable Collector Trap
List<String> ids = new ArrayList<>();
orders.parallelStream()
.filter(Order::paid)
.forEach(order -> ids.add(order.id()));
ArrayList is not safe for concurrent writes. Results may be lost or internal state corrupted. Removing parallel() does not make the shape good; it only hides the immediate race.
Return a value from the pipeline:
List<String> ids = orders.stream()
.filter(Order::paid)
.map(Order::id)
.toList();
Ownership is obvious. The stream produces a list.
peek Is Not An Event Hook
peek exists mainly for observing a pipeline during debugging. A business side effect inside it is easy to skip accidentally because streams are lazy.
orders.stream()
.peek(audit::record)
.filter(Order::paid);
// no terminal operation, so nothing runs
Even with a terminal operation, reordering or short-circuiting can change which elements reach the effect. Put audit behavior in an explicit loop or service operation where its guarantee can be tested.
Ordering Must Be Deliberate
forEach on a parallel stream does not preserve encounter order. forEachOrdered does, but coordination may remove the performance benefit you expected.
If order is business data, model it before the effect:
List<Invoice> due = invoices.stream()
.filter(Invoice::isDue)
.sorted(comparing(Invoice::deadline))
.toList();
for (Invoice invoice : due) {
sender.send(invoice);
}
Transformation and effect now have a visible boundary.
Parallel Is Not A Free Speed Button
Parallel streams use a shared fork-join pool by default. Blocking database and HTTP calls can occupy workers in ways unrelated components did not expect. Small collections also pay splitting and coordination overhead for no useful gain.
Use parallel streams for measured CPU-bound work with independent elements. For request-scoped I/O concurrency, choose an API where cancellation, timeouts, and task ownership are explicit.
A Plain Loop Can Be Better
A loop is not failure of functional programming. When work has effects, branching recovery, or careful ordering, a loop often tells truth more clearly.
Use streams when the sentence is “filter, map, group, reduce.” Stop when the sentence becomes “call, retry, mutate, audit, and maybe compensate.”