Skip to main content

StreamSupport.stream creates a new sequential or parallel Stream from a Spliterator (which in turn can be obtained from any Collection). Conditionally you can switch parallel processing on or off.

boolean parallel = true;
StreamSupport.stream(IntStream.range(0, 10).spliterator(), parallel).forEach(...);

Of course, you can also if-else if you have such a flag:

boolean parallel = findOutIfParallelizingIsWorthIt();
Stream<T> myStream = parallel ? myCollection.parallelStream() : myCollection.stream();
myStream.forEach(...);