Performing Stream Operations with a Single Pass of a Parallel Stream

Lines 51–60 demonstrate the performance improvement you get by using LongStream method summaryStatistics on a parallel LongStream. To obtain a parallel stream that can take advantage of multi-core processors, simply invoke method parallel on an existing stream. As you can see from the sample output, performing the operations on a parallel stream decreased the total time required even further—taking approximately 55% of the calculation time for the sequential LongStream and just 22% of the time required to perform the five operations separately.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.