O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

As mentioned in the previous recipe, a parallel stream can be created by the parallelStream() method of a collection or the parallel() method applied to a stream. Conversely, the existing parallel stream can be converted into a sequential one by calling the stream using the sequential() method.  

As the first best practice, use a sequential stream by default and start thinking about the parallel one only if you have to and you can. The have to usually comes up if the performance is not good enough and a large amount of data has to be processed. The possibilities are limited by the nature of the stream source and operations. Some of them cannot be either processed in parallel or produce non-deterministic results.  For example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required