100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java Mastery
35 minintermediate

Streams API: Filter, Map, Reduce

The Streams API, introduced in Java 8, is a declarative way to process sequences of data. Instead of writing loops that say how to iterate, you describe what you want as a pipeline of operations, filter these, map them to that, collect the result, and the library handles the mechanics. A stream is not a data structure that stores elements; it is a view over a source (a collection, an array, a generator) that carries elements through a chain of operations.

A stream pipeline has three parts: a source, zero or more intermediate operations (like filter and map) that are lazy and return a new stream, and exactly one terminal operation (like collect, forEach, or reduce) that triggers processing and produces a result. The lazy, build-then-run design enables important optimisations and a clean, composable style.

Understanding streams matters because they have become the idiomatic way to transform and aggregate data in Java, replacing verbose loops with readable pipelines that express intent directly. Grasping the source-intermediate-terminal structure, the laziness of intermediate operations, and the core operations, filter, map, reduce, and collect, equips you to write concise, declarative data processing that is often clearer and sometimes faster than the equivalent loops.

Analogy🏏Cricket
🏏 Think of it like cricket: a team sheet does not just list players, it assigns each to a precise, declared role, opener, spinner, wicketkeeper, and the laws and the captain enforce that a player operates within their declared role: you cannot send a designated bowler to keep wicket without an official change. Just as each player's role is fixed and checked before play, each Java variable's type is fixed at compile time and checked by the compiler. Just as trying to use a player outside their role is caught by the officials before it disrupts the match, using a variable in a type-incompatible way is caught by the compiler before the program runs. Just as clear role assignments prevent on-field confusion, clear type declarations prevent runtime errors. The insight is that declaring and enforcing roles up front, for players or for data, catches mistakes early rather than mid-match.
Lesson 17 of 35
0% complete