Java Stream Intermediate And Terminal Operations Cheat Sheet

Credits

Java 8 Stream has many operations which can be pipe lined together to get desired result. Some operations produce another stream as a result and some operations produce non-stream values as a result. The operations which return another stream as a result are called intermediate operations and the operations which return non-stream values like primitive or object or collection or return nothing are called terminal operations. In this post, we will see the differences between Java 8 Stream intermediate and terminal operations.

Also Read : Solving Real Time Queries Using Java 8 Features – Employee Management System

Java 8 Stream Intermediate And Terminal Operations :

1) The main difference between intermediate and terminal operations is that intermediate operations return a stream as a result and terminal operations return non-stream values like primitive or object or collection or may not return anything.

2) As intermediate operations return another stream as a result, they can be chained together to form a pipeline of operations. Terminal operations can not be chained together.

3) Pipeline of operations may contain any number of intermediate operations, but there has to be only one terminal operation, that too at the end of pipeline.

Also Read : Collections Vs Streams

4) Intermediate operations are lazily loaded. When you call intermediate operations, they are actually not executed. They are just stored in the memory and executed when the terminal operation is called on the stream.

5) As the names suggest, intermediate operations doesn’t give end result. They just transform one stream to another stream. On the other hand, terminal operations give end result.

6) Intermediate Operations :

map()filter()distinct()sorted()limit()skip()

Terminal Operations :

forEach()toArray()reduce()collect()min()max()count()anyMatch()allMatch()noneMatch()findFirst()findAny()

Also Read : Java 8 map() Vs flatMap()

Below is the list of intermediate and terminal operations.

Java 8 Stream Intermediate And Terminal Operations

Also Read : 50+ Java Threads Interview Questions And Answers

Java 8 Stream Intermediate Vs Terminal Operations

Intermediate OperationsTerminal Operations
They return stream.They return non-stream values.
They can be chained together to form a pipeline of operations.They can’t be chained together.
Pipeline of operations may contain any number of intermediate operations.Pipeline of operations can have maximum one terminal operation, that too at the end.
Intermediate operations are lazily loaded.Terminal operations are eagerly loaded.
They don’t produce end result.They produce end result.
Examples :
filter(), map(), distinct(), sorted(), limit(), skip()
Examples :
forEach(), toArray(), reduce(), collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny()
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments