Understanding Java Streams Operations

In this tutorial, we’ll walk through the Optional class methods that were introduced in Java 8 and improved with new features in the last versions of Java.

What is?

A Stream is a sequence of elements supporting parallel and aggregate operations. Further, Java 8 supports processing streams through operations defined in a declarative way. These operations are linked-up according to the principle of pipelines and they access the elements via Internal Iterations.

Structure of Java Stream Operations

The basics concepts of Stream API are – a source, one or more intermediate operations, and a terminal operation. These three types of components are pipelined in a sequence to make a stream work.

Source -> Intermediate operations -> Terminal operation 

Let us now understand the three concepts of stream operations shown above –

Lazy execution of Streams

Streams are executed in lazy way and optimize the process by taking a high level view of the entire set of pipelined operations and then applying the optimization techniques to improve efficiency of execution. Let us first take an example to understand the lazy nature of stream execution:

Stream.iterate(0, n -> n + 2)
    .peek(value -> System.out.println("Value at: " + value))
    .limit(10)
    .forEach(System.out::println);

The output of the above code:

Value at: 0
0
Value at: 2
2
Value at: 4
4
Value at: 6
6
..
Value at: 16
16

In the example, iterate() helps to generate an infinite number of even numbers starting from the initial value provided, which is 0. Imagine if streams were not optimized, so iterate() would normally generate a large number of integers, which should have been passed to the intermediate operations. Finally, the stream would have been limited to just 10, and only the first 10 of this huge list of numbers should have been printed using forEach() method. Instead, there are multiple optimizations at play here which make the above Stream efficient –

Types of Intermediate Operations

Intermediate operations can be categorized into two categories –

Conclusion

In this article, we saw what Java Streams are, understood the inner working, and saw the concepts and various terms that describe a Stream.