Skip to content

Chapter 28 of 37

Streams API

Build lazy pipelines with filtering, mapping, reduction, collection, and parallel execution.

44 minutes 10 quick checksBy Subha Prasad
Lesson 28 of 37Course navigation

Lesson content

Read, practise, then check your understanding

A Stream is a single-use lazy pipeline, not a collection. Intermediate operations build the pipeline; a terminal operation consumes it.

Declarative transformation

List<String> result = names.stream()
    .filter(name -> !name.isBlank())
    .map(String::trim)
    .distinct()
    .sorted()
    .toList();

Use map for one-to-one transformation, flatMap for nested sources, and reduce/collectors for aggregation. Avoid stateful side effects and modifying the source. Parallel streams require associative operations, splittable data, sufficient work, and careful common-pool impact; measure before enabling. Streams holding I/O resources require try-with-resources.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Stream?
Which Java term matches this description: A lazy pipeline over elements rather than a data structure.
Which statement best describes intermediate operation?
Which Java term matches this description: A lazy pipeline step such as filter or map.
Which statement best describes terminal operation?
Which Java term matches this description: An operation such as collect or reduce that consumes a stream.
Which statement best describes reduce?
Which Java term matches this description: An operation combining stream elements into a result.
Which statement best describes parallel stream?
Which Java term matches this description: A stream whose operations may execute across fork-join tasks.

0 of 10 checks passed

Your progress is saved on this device.

Streams API | Java Lesson | Subha Prasad