Skip to main content

Command Palette

Search for a command to run...

Stream in Java (with examples)

Updated
4 min read
Stream in Java (with examples)
M

Software engineer turned technical writer.

Streams have been introduced in Java 8, and like lambda expression, they represent an attempt to bring functional programming to Java.

What is a stream?

A Stream is an interface part of the java.util.stream package. It is a series of objects with several methods that can be executed sequentially to obtain a certain result.

Don’t confuse Streams with Java I/O streams. It is a different topic!

The traits of a stream are:

  • A sequence of elements: a stream is executed on demand. It doesn’t store any data nor modify the provided data source.

  • Data source: the type of input that a stream can operate on. It takes Collections, Arrays, or Input/Output resources.

  • Pipelining: a stream is a chain of intermediate operations that end with a terminal operation (an operation that cannot be linked to another operation).

  • Aggregate operations: a stream supports many methods that can be linked together (this is discussed after).

  • Automatic iterations: stream operations perform all the iterations in the background.

Let’s take a look at the below example:

import java.util.Arrays;
import java.util.List;

public class StreamAPI {

    public static void main(String[] args) {

        List<String> animals = Arrays.asList("dog", "cat", "cow", "Fox", "froG", "fiSh");

        animals
                .stream()
                .filter(a -> a.startsWith("f"))
                .map(String::toLowerCase)
                .sorted()
                .forEach(System.out::println);

    }
}

The output is:

fish
frog

In the code above:

  1. You create a List of Strings called animals. This list of animals has a dog, a cat, a cow, a Fox, a froG, and a fiSh. Some letters have been written into capitals on purpose to better showcase what you’re trying to achieve.

  2. You perform a sequence of operations. First, we use the filter() method to say that we only want those animals that start with the letter “f”.

  3. You use the map() to turn those animals into lowercase.

  4. You use the sorted() method to put them in their natural order.

  5. In the end, we print them.

In short, this whole operation says “Please Java, give me a stream of those animals that start with f, lowercase and in alphabetical order”. The uppercase letters in frog and fish get converted into lowercase letters.

Intermediate and terminal operations

In the following code snippet above:

import java.util.Arrays;
import java.util.List;

public class StreamAPI {

    public static void main(String[] args) {

        List<String> animals = Arrays.asList("dog", "cat", "cow", "Fox", "froG", "fiSh");

        animals
                .stream()
                .filter(a -> a.startsWith("f"))
                .map(String::toLowerCase)
                .sorted()
                .forEach(System.out::println);

    }
}

There are intermediate and terminal operations.

Intermediate operation

Intermediate operations can be connected and return a stream as a result.

Intermediate operations are lazily loaded, meaning they are stored in the memory and only get executed when the terminal operation is called at the end of the pipeline.

Examples of intermediate operations are:

  • Filter: the filter method retrieves only certain elements based on given criteria. We used this method to fetch only those animals that start with the letter “f”.

  • Map: the map method is used to change the elements of a stream by applying a function. Before, we used the toLowerCase() method reference to convert the elements into lowercase letters, and indeed, you got frog and fish in lowercase.

  • Sorted: the sorted method sorts the elements in their natural order. Indeed, the program above printed fish first and frog after.

  • Limit: it simply returns a stream that contains the top N elements.

Terminal operations

Terminal operations cannot be chained together, and they return non-stream values.

They are responsible for producing the end result.

Examples of terminal operations are:

  • ForEach: this method is used to iterate through each element. You used the forEach() to print all the filtered, converted, and sorted animals.

  • ToArray: returns an array containing items of a stream.

  • Collect: used to gather the result of processing.

  • Count: it returns the number of elements in a stream.

Key Takeaways

After reading this article, you'll learn about streams, intermediate and terminal operations.

Continue reading the Java section on this blog to learn more!

Until next time! 😁

Y

Great article Maddy. Thank you for your effort ♥️ I have this concern when it is said there can be one terminal operation and terminal operations cannot be chained. we can write something like this right? Isn’t this chaining collect and forEach which are both terminal operations. I don’t get that part 🤔 Stream1.map().collect().forEach()

D

Another great one Maddy, going to complete the series!

Just a suggestion: The explanation, as it is a step-by-step process, could have been done using some bullet/numbered list for better reading.

Great content, keep it up!

1
M
Maddy4y ago

Thanks for the suggestion, I'll keep this in mind for my next article. 😊

1

More from this blog

T

Tech with Maddy | Tech and Writing

122 posts