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:
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.
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 methodretrieves
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 thetoLowerCase()
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 theforEach()
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! 😁