Intermediate stream operations are lazy; the terminal operation starts the single-use pipeline.
Build a Complete Mental Model
A stream is not a data structure but a lazy, single-use processing pipeline over a source. Prefer stateless non-interfering operations, minimize side effects, and do not use parallel streams without measurement and correctness analysis.
Apply It in Real Java
Transform a collection with filter, map, and collect, proving the source is unchanged and work stays lazy until a terminal operation.
After This Lesson
import java.util.List;
class Main {
public static void main(String[] args) {
List<String> names = List.of("ada", "grace", "linus");
List<String> result = names.stream()
.filter(name -> name.length() >= 5)
.map(String::toUpperCase)
.toList();
System.out.println(result);
}
}[GRACE, LINUS]Try It Yourself
Transform a collection with filter, map, and collect, proving the source is unchanged and work stays lazy until a terminal operation.
Common Mistake
Reusing a consumed stream or mutating shared state in forEach while assuming parallel safety.
Java Stream API — Oracle