Java SE 11 Developer (1Z0-819) — Question 44
Why would you choose to use a peek operation instead of a forEach operation on a Stream?
Answer options
- A. to process the current item and return void
- B. to remove an item from the end of the stream
- C. to process the current item and return a stream
- D. to remove an item from the beginning of the stream
Correct answer: C
Explanation
The correct answer is C because the peek operation allows you to process each element of the stream while still returning the original stream for further operations. In contrast, forEach performs an action on each element without returning a stream, and options A, B, and D do not accurately describe the functionality of peek.