Sobes.tech
Junior — Middle

Kas on Java forEach meetod lõpp-punkt või vahepealne etapp voogude töötlemisel?

sobes.tech AI

Vastus AI-lt

In Java, the forEach method for streams (Stream.forEach) is a terminal operation, meaning it is the endpoint of stream processing. After calling forEach, the stream is considered consumed and no further operations can be performed on it.

Example:

Stream.of(1, 2, 3)
      .filter(n -> n > 1)  // intermediate operation
      .forEach(System.out::println);  // terminal operation

Here, filter is an intermediate operation, and forEach is the terminal operation, which initiates the execution of the entire pipeline and outputs the elements.