Difference Between Intermediate And Terminal Operations In Java
Java 8 Stream includes a variety of operations that can be combined to produce the desired outcome. Some operations result in the creation of another stream, while others result in the creation of values that are not streams. Operations that produce another stream as a result are referred to as intermediate operations, whereas operations that produce non-stream values, such as primitive, object, or collection values, or activities that produce nothing at all are referred to as terminal operations. The distinctions between Java 8 Stream intermediate and terminal operations will be covered in this post.
The primary distinction between intermediate and terminal operations is that the latter return non-stream values, such as primitives, objects, collections, or nothing at all, whereas the former return a stream as a result.They can be linked together to create an operation pipeline because intermediary operations always result in the return of another stream. It is not possible to chain together terminal actions.
Any number of intermediate operations may be present in a pipeline of activities, but only one terminal operation, and that too at the pipeline's terminus, is required.
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.
As their names imply, intermediate procedures do not produce final results. They only change one stream into another. On the other hand, terminal processes produce a final product.
Example
Intermediate Operations :
map(), filter(), distinct(), sorted(), limit(), skip()
Terminal Operations :
forEach(), toArray(), reduce(), collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny()
Operation | Return Type | Type Of Operation | What It Does? |
filter() | Stream<T> | Intermediate | Returns a stream of elements which satisfy the given predicate. |
map() | Stream<R> | Intermediate | Returns a stream consisting of results after applying given function to elements of the stream. |
distinct() | Stream<T> | Intermediate | Returns a stream of unique elements. |
sorted() | Stream<T> | Intermediate | Returns a stream consisting of elements sorted according to natural order. |
limit() | Stream<T> | Intermediate | Returns a stream containing the first n elements. |
skip() | Stream<T> | Intermediate | Returns a stream after skipping the first n elements. |
forEach() | void | Terminal | Performs an action on all elements of a stream. |
toArray() | Object[] | Terminal | Returns an array containing elements of a stream. |
reduce() | type T | Terminal | Performs a reduction operation on elements of a stream using an initial value and a binary operation. |
collect() | Container of type R | Terminal | Returns a mutable result container, such as a List or Set. |
min() | Optional<T> | Terminal | Returns the minimum element in a stream wrapped in an Optional object. |
max() | Optional<T> | Terminal | Returns the maximum element in a stream wrapped in an Optional object. |
count() | long | Terminal | Returns the number of elements in a stream. |
anyMatch() | boolean | Terminal | Returns true if any one element of a stream matches the given predicate. |
allMatch() | boolean | Terminal | Returns true if all the elements of a stream match the given predicate. |
noneMatch() | boolean | Terminal | Returns true only if none of the elements of a stream match the given predicate. |
findFirst() | Optional<T> | Terminal | Returns the first element of a stream wrapped in an Optional object. |
findAny() | Optional<T> | Terminal | Returns any one element in a stream wrapped in an Optional object. |
Java 8 Stream Intermediate Vs Terminal Operations
Intermediate Operations | Terminal Operations |
They return a stream. | They return non-stream values. |
They can be chained together to form a pipeline of operations. | They can’t be chained together. |
A pipeline of operations may contain any number of intermediate operations. | A pipeline of operations can have a maximum of one terminal operation, which must be at the end. |
Intermediate operations are lazily loaded. | Terminal operations are eagerly loaded. |
They don’t produce an end result. | They produce an end result. |
Examples: filter(), map(), distinct(), sorted(), limit(), skip() | Examples: forEach(), toArray(), reduce(), collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny() |