Chapter EIGHTEEN
Parallel Streams


Exam Objectives

Use parallel Streams including reduction, decomposition, merging processes, pipelines and performance.

Answers

1. The correct answer is B.
Sums are associative operations, therefore, the reduce() method ensures that the stream can be processed in parallel while preserving the order of its elements.


2. The correct answer is C.
Option A is false. Collections use the parallelStream() method.
Option B is false. Operations that are computationally expensive, generally have a better performance using a parallel stream.
Option C is true. filter() is considered stateless.
Option D is false. Parallel streams don't always perform better than sequential streams.


3. The correct answer is A.
If you remember from the previous chapter, the average(), sum(), max(), and min() methods are implemented as a reduce operation with the collect() method, so the stream can be safely processed in parallel.


4. The correct answer is D.
The call of sequential() turns the whole stream pipeline into a sequential one. In general, we can say that the last call to parallel() or sequential() is the one that affects the whole stream pipeline.