Answer: The Stream API supports functional-style, declarative processing of sequences of data: filter -> map -> terminal operation. Streams do not store data and are lazily evaluated.
import java.util.*;
class Employee {
String name; double salary;
Employee(String n, double s) { name = n; salary = s; }
}
public class StreamEmp {
public static void main(String[] args) {
List<Employee> list = Arrays.asList(
new Employee("Asha", 60000), new Employee("Ravi", 45000),
new Employee("Meena", 72000));
list.stream()
.filter(e -> e.salary > 50000)
.map(e -> e.name.toUpperCase())
.forEach(System.out::println);
}
}
Output:
ASHA
MEENA
Declarative processing with streams. The Stream API introduced in Java eight processes sequences of elements in a functional style. A stream pipeline begins with a source such as a collection, array or generator, continues with zero or more intermediate operations and ends with one terminal operation. Intermediate operations such as filter, map, sorted, distinct and limit are lazy: they build a description of the work and do not process elements until a terminal operation is invoked. Terminal operations such as forEach, count, collect, reduce, min,max, sum and anyMatch trigger evaluation and produce a result or a side effect. Streams do not store data themselves and they do not modify the source collection. A stream can be consumed only once, so a new stream must be created for a second pass. Primitive specialisations such as IntStream avoid boxing and provide sum directly. To sum even numbers from one to ten, the program builds a list, calls stream, filters values divisible by two, converts to an IntStream with mapToInt and calls sum, producing thirty. To process Employee objects, the stream filters salaries above a threshold, maps each employee name to upper case and prints names with forEach. A good answer defines the pipeline structure, explains laziness and single use, distinguishes intermediate from terminal operations and writes both requested programs with their outputs. Mentioning that parallelStream can divide work across threads but is worthwhile only for large independent tasks adds practical balance. Noting that streams encourage immutable style and replace verbose loops with composable operations gives the design justification.
Result: filter is an intermediate operation and forEach is terminal; only employees with salary > 50000 are printed in uppercase.
Tip from Examiner: State the principle or language rule, write the code or structure with key terms defined, explain the execution or design decision and support the answer with a labelled diagram or comparison where useful.