Stream (Java)

A Stream is a way to process a sequence of data through a pipeline of operations like filter, map, and collect. It lets you transform collections in a clean, declarative style instead of writing manual loops.

Java

Streams, introduced in Java 8, don't store data themselves — they flow over a source like a List and apply operations to each element. They pair naturally with lambda expressions.

A stream pipeline has a source, zero or more intermediate operations (like filter and map), and a terminal operation (like collect, count, or forEach) that produces a result.

Examples

Java

import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4);
        List<Integer> evens = nums.stream()
            .filter(n -> n % 2 == 0)
            .collect(Collectors.toList());
        System.out.println(evens);
    }
}

Output:

[2, 4]

Learn it in a lesson

See also

Back to the Dictionary