Analyze the following code, which processes a list using a parallel stream. Which statement is true?

Java interview question for Advanced practice.

Answer

The code is correct and does not have a concurrency issue.

Explanation

The code is correct. String objects are immutable, so String::toUpperCase is inherently thread-safe. Furthermore, the collect(Collectors.toList()) terminal operation is designed to work safely with parallel streams. The stream pipeline's collector uses thread-safe intermediate data structures to gather results from worker threads before merging them into the final ArrayList. Therefore, no external synchronization is needed and there is no concurrency bug in this code.

Related Questions