Java 8 added lambda expressions and the Stream API for declarative collection processing—filter, map, reduce without manual index loops.
Lambda syntax
list.forEach(s -> System.out.println(s));
list.sort((a, b) -> a.compareToIgnoreCase(b));
Stream pipeline
long count = names.stream()
.filter(s -> s.startsWith("A"))
.map(String::toUpperCase)
.count();
Streams are lazy until a terminal operation (collect, count, forEach) runs.
Important interview questions and answers
- Q: Stream vs parallelStream?
A: parallelStream uses fork/join—only beneficial on large CPU-bound workloads with thread-safe ops. - Q: Method reference?
A: Shorthand lambda—String::toUpperCaseequivalent tos -> s.toUpperCase().
Self-check
- What triggers stream evaluation?
- Name two intermediate stream operations.
Pitfall: Streams are single-use—calling a terminal operation twice on the same stream throws IllegalStateException.
Interview prep
- When use streams?
Declarative collection transforms—filter/map/reduce—especially when chaining operations; simple loops are fine for trivial cases.