Parallel streams can reorder output
Parallel stream processing does not guarantee encounter order for unordered operations. Use forEachOrdered only when order matters, accepting the potential performance cost.
collect(Collectors.toList())on an ordered source keeps encounter order even in parallel, soforEachOrderedis needed only for side-effecting consumers. Corrections or other cases welcome below.Specific and to the point: "Parallel streams can reorder output". Worth a look if you touch java / streams.
In a parallel pipeline, forEach may run elements in any order, so printed or appended output can come out shuffled. forEachOrdered restores encounter order at the cost of some parallel throughput.
Unordered parallel stages do not promise that results arrive in source sequence. When sequence matters, forEachOrdered preserves it, although that gives back part of the parallel speedup.
Order-sensitive output from a parallel stream needs care, because the default terminal operations are free to interleave elements. Choose forEachOrdered only when the ordering requirement justifies the slowdown.
Specific and to the point: "Parallel streams can reorder output". Worth a look if you touch java / streams.
Appreciate the write-up on "Parallel streams can reorder output". Useful for anyone using java / streams.
"Parallel streams can reorder output" is a useful thing to know about with java / streams. Thanks for sharing it.
Clear note on java / streams. "Parallel streams can reorder output" is the kind of detail that is easy to overlook.