Lesson 5 of 618 minModule progress 0%

Module 13: Functional Programming and Streams

Terminal Operations and Collecting Results

Finish pipelines with collecting, counting, matching, and simple reductions.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-17

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, manually edited for clarity, and checked against current Java documentation and runnable examples.

Learning goals

  • Recognize when a terminal operation ends the stream
  • Choose the right output for the job
  • Use collectors without overcomplicating the pipeline

Terminal operations trigger execution: Until a terminal step runs, the stream pipeline is just a description.

Common terminals are practical and enough for most work: toList(), count(), forEach(), findFirst(), and reduce() cover a lot of ground.

Collectors shine when you want structure: Grouping, joining, or mapping into collections becomes easier and less error-prone.

Pick the simplest ending that matches the outcome you need: Avoid fancy collectors when a count or list is enough.

Runnable examples

Count matching items

long count = java.util.List.of(3, 6, 8, 10).stream()
    .filter(number -> number > 5)
    .count();

System.out.println(count);

Expected output

3

Mini exercise

Use a stream to count how many names in a list start with the letter `A`.

Summary

  • Terminal operations are what run the pipeline.
  • Use the simplest result shape that solves the problem.
  • Collectors are helpful, but not every stream needs a complex collector.

Next step

Close the module with a stream-based reporting project.

Sources used

Advertisement

Lesson check

What is true about terminal operations?

Next lesson →