Introduced in Java 8, the Stream API transformed how developers process collections. It replaces error-prone imperative loops (for, if, accumulators) with clean, declarative, high-speed data processing pipelines.
Figure 10: Stream Lifecycle: Source → Lazy Intermediate Transformations → Eager Terminal Reduction.
Suppose we need to take a list of employees, filter those earning > $50,000, sort them by name, and collect their uppercase names into a new List:
import java.util.List;
import java.util.stream.Collectors;
record Employee(String name, String department, double salary) {}
public class StreamMasterclassDemo {
public static void main(String[] args) {
List<Employee> staff = List.of(
new Employee("Aarav", "IT", 75000),
new Employee("Pooja", "HR", 45000),
new Employee("Vikram", "IT", 95000),
new Employee("Sneha", "Finance", 80000)
);
// Declarative Stream Pipeline:
List<String> topItEarners = staff.stream()
.filter(e -> e.salary() > 50000) // Intermediate: Filter
.filter(e -> e.department().equals("IT")) // Intermediate: Filter
.map(Employee::name) // Intermediate: Transform to String
.map(String::toUpperCase) // Intermediate: Uppercase
.sorted() // Intermediate: Sort alphabetically
.collect(Collectors.toList()); // Terminal: Collect to List
System.out.println("Top IT Earners: " + topItEarners); // [AARAV, VIKRAM]
}
}
reduce() and Numerical SumsList<Integer> numbers = List.of(1, 2, 3, 4, 5);
// Sum using reduce: (identity, accumulator)
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println("Sum of numbers: " + sum); // 15
Exam Question: "Can a Java Stream be reused after a terminal operation has executed?"
Answer: No! A Java Stream is a one-time consumable pipeline. The moment a terminal operation (like collect() or count()) executes, the stream is closed and consumed. Attempting to invoke another operation on that same stream instance throws: IllegalStateException: stream has already been operated upon or closed.
Stream operations like findFirst() return an Optional. Let us explore how Optional eliminates NullPointerExceptions. Proceed to: Java Optional Class →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Predicates and functions in streams.
Returned by stream reduction operations.
Stream sources.
Functional pipeline logic.