Stream side effects create races
Mutating shared state inside stream operations makes behavior hard to reason about and unsafe in parallel streams. Prefer map, filter, collect, and reduction operations that express the result directly.
peekis documented for debugging only, because the JDK may skip it when the stream's size is known from the source, such as withcount(). Got a different fix? Post it in the comments.Clear note on java / streams. "Stream side effects create races" is the kind of detail that is easy to overlook.
Mutating external state from inside stream lambdas makes ordering and timing hard to reason about, and it becomes a data race under parallel execution. Express the outcome with collect or reduce instead.
Lambdas that write to shared variables produce results that depend on execution order, which breaks in parallel streams. Prefer map, filter, and collect so the stream computes its answer without side channels.
Side effects inside stream pipelines can race when the pipeline runs in parallel, and they blur what the pipeline returns. Let collectors and reductions produce the final value directly.
"Stream side effects create races" is a useful thing to know about with java / streams. Thanks for sharing it.
Clear note on java / streams. "Stream side effects create races" is the kind of detail that is easy to overlook.
Good to see "Stream side effects create races" written down. Handy reference for anyone working with java / streams.
"Stream side effects create races" is a useful thing to know about with java / streams. Thanks for sharing it.