0 votes
in JAVA by

What is the difference between peek() and forEach() in a stream pipeline?

a) peek() performs a side-effect on each element of the stream, while forEach() terminates the stream and performs a side-effect on each element.

b) peek() terminates the stream and performs a side-effect on each element, while forEach() performs a side-effect on each element of the stream.

c) peek() and forEach() are identical methods.

d) peek() and forEach() can only be used with parallel streams.

1 Answer

0 votes
by

a) peek() performs a side-effect on each element of the stream, while forEach() terminates the stream and performs a side-effect on each element.

Explanation: The peek() method allows you to perform a side-effect on each element of the stream, without modifying the stream. The forEach() method performs a side-effect on each element of the stream, and terminates the stream.

Related questions

0 votes
asked Jul 6, 2023 in Azure by sharadyadav1986
0 votes
asked Apr 28 in JAVA by Robin
...