Stream in Java (with examples)

Search for a command to run...

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()
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!
This article will explain the following: What a lambda expression is. External VS Internal iteration. Lambda expression within a functional interface. Benefits of using a lambda expression. What is a lambda expression? Java 8 has introduced man...
AI has transformed how I work as a technical writer. Large tech companies are developing and sharpening these tools, but some of them are better than others depending on what you want to accomplish. T

I've been writing professionally for 3+ years, and on this personal blog for 5+ years. Throughout this time, I've built a large network made of developers, other technical writers, solutions engineers

I’ve been writing online for almost five years, with a professional focus on technical writing for over two years. If you struggle to create technical content that is easy to digest, this article is for you. Where do I start? If you don’t know where ...

LinkedIn is the only social networking site purposely built to help job seekers. Over the years, LinkedIn has evolved by offering two types of experiences: Job searching Content creation This article will describe how to maximize your LinkedIn fo...

In the last 12+ months as a technical writer, I've worked on several projects highlighting three technical writing elements. This article will introduce you to: Information Architecture Docs-as-code Content strategy Information Architecture (IA)...

Streams have been introduced in Java 8, and like lambda expression, they represent an attempt to bring functional programming to Java.
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:
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.
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”.
You use the map() to turn those animals into lowercase.
You use the sorted() method to put them in their natural order.
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.
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 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 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.
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! 😁