Kotlin
Java

Streams
Java

Created By: Geoffrey Challen
/ Updated: 2022-05-23

In this lesson we'll introduce a new programming paradigm that is particular useful for working with data: streams. We'll manipulated linear collections of data using good old for loops. But we can do better. Let's see how!

Iterative Building Blocks

Many of the code and algorithms we've written together have operated on sequential data, stored in either arrays or Lists. And we've seen and identified common patterns for working with this kind of data. Such as counting:

And searching:

And transforming:

And filtering:

And combining:

And while these are fine building blocks for creating larger programs, there is something a bit repetitive and dull about them. Every one has the same overall structure: the same loop, the same return structure. Shouldn't there be a better, more compact way of expressing these kind of patterns?

Streams

Yes. There is. And in Java these are called streams.

Streams allow us to work with sequential data by composing powerful programming primitives to great effect. First, let's examine the Javadoc together.

Go over the Javadoc for the Stream class.

Stream Operations

Next, let's examine how to utilize common stream operations to replace the repetitive loop-based code we wrote above. First, let's look at how to set up a stream and one of the most basic stream operations—map:

Show how to create a stream, how to use map, and how to convert a Stream to a list or use forEach. Discuss Stream termination.

We can also filter streams using... filter. And count them using... count! Let's see how:

Describe Stream filter.

And, we can even reduce a Stream until a single value with reduce, a surprisingly powerful primitive.

Show how to use reduce.

Why Streams?

Streams may seem alien to you at first. That's not surprising. A famous silicon valley tech thought leader has pointed out that powerful programming ideas usually feel strange and even bizarre at first. But, as you come to appreciate them, not only do they become more natural, but the older less-powerful ways of doing things start to see even more limited.

Compared to for loops, Streams are:

  • More succint: the common parts of the various for-loop based patterns we've listed above have been factored out, leaving only the decision-making logic that changes depending on the application
  • More composable: it is easier to reorder stream operations to accomplish different data processing tasks than it is to tease apart different parts of for loop
  • More efficient: Streams allow various kinds of operations (like map, for example) to be done in parallel, increasing the speed with which large collections can be processed

Stream Example

To wrap up, let's have some fun with Streams working with one of our favorite data sets:

Do a few Stream operations using collections of Dogs.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.