1Z0-830 practice question 9 of 25

Which of the following statements is true about using parallel streams with a List collection?

Full set: 116 questions
Answer options
A:

A. Parallel streams always process elements in order.

B:

B. Parallel streams guarantee thread-safety for modifications to the original List. 33/97

C:

C. Parallel streams can improve performance for large collections.

Correct answer
D:

D. Parallel streams require explicit synchronization for all operations.

Correct Answer:
C. C. Parallel streams can improve performance for large collections.
Explanation

Parallel streams are designed to leverage multi-core processors to perform operations on collections concurrently. For large collections, this can lead to significant performance improvements by dividing the work among multiple threads. The overhead of managing the parallel execution is offset by the gains from concurrent processing. However, for small collections, the overhead might make parallel streams slower than their sequential counterparts.

Why Incorrect

A. Parallel streams do not guarantee the order of processing. Elements are processed concurrently, and their completion order can be non-deterministic unless an order-preserving terminal operation is used.
B. Parallel streams do not add any thread-safety guarantees. If the stream's operations modify the source List, it is the programmer's responsibility to ensure thread-safety.
D. Explicit synchronization is only required if the stream's operations involve side effects that modify shared, mutable state. Stateless lambda expressions do not require synchronization.

References

1. Oracle, "The Java™ Tutorials: Parallelism." This tutorial states, "You can use parallel streams to improve performance for bulk operations that process elements of a collection." (Section: Parallelism).

2. Oracle, "The Java™ Tutorials: Aggregate Operations." It notes, "However, parallelism is not automatically faster than performing operations serially... though it is easy to enable parallelism, you can actually make your application perform more slowly if you use it incorrectly." (Section: Parallelism).

3. Goetz, B., et al. (2006). Java Concurrency in Practice. Addison-Wesley. Chapter 7 discusses the fork-join framework, which underlies parallel streams, and its performance characteristics.

Scroll to Top