Sobes.tech
Junior — Middle

Kas andmevoog toimib ilma lõppoperatsiooni käivitamata?

sobes.tech AI

Vastus AI-lt

In Java, a data stream (stream) will not be executed without calling a terminal operation. Streams in Java are lazy: intermediate operations (e.g., map, filter) only describe transformations but are not executed immediately.

Example:

Stream<Integer> stream = Stream.of(1, 2, 3, 4)
    .filter(x -> x % 2 == 0)
    .map(x -> x * x);
// Nothing happens here because there is no terminal operation

List<Integer> result = stream.collect(Collectors.toList()); // Terminal operation, starts calculations

Without invoking a terminal operation (collect, forEach, reduce, etc.), the stream will not be processed, and calculations will not occur.