Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

6 Difference between Intermediate and Terminal Operations in Java

In this article, we will be discussing the differences between Intermediate and Terminal operations in Java. In Java 8, Stream API was introduced. Stream API operations are of two types:

1. Intermediate operations

2. Terminal operations

Intermediate operations process the current stream of data (if any) and then return a new stream. Terminal operations as the name suggests are the last in the pipeline of operations performed on a stream. To know more about Streams API, look at the article Top 10 Stream API interview questions with answers.

Read Also: Java 8 Online Quiz

Difference between Intermediate and Terminal Operations in Java

1. Return type


Intermediate operations return a stream itself. Example is:
 StreamT> distinct()

Terminal operations produce either a value or a side-effect.
 OptionalT> findAny()


2. Chaining


An intermediate operation returns a new stream , they can be chained. A stream can execute any number of intermediate operations, which is termed stream chaining.

A stream can have only one terminal operation, it cannot be chained.

 import java.util.stream.*;
import java.util.*;

public class IntermediateTerminalOperations {
public static void main(String args[]) {

Stream stream1 = Stream.of("a", "b", "c", "d");

/* Intermediate operations are sorted() and map(),
where as terminal operation is collect() */

List list1 = stream1.sorted().map(s -> s.toUpperCase()).collect(Collectors.toList());

Stream stream2 = Stream.of("e", "f", "g", "h");

/* Intermediate operations are map() and sorted(),
where as terminal operation is forEach() */

stream2.sorted().map(s2 -> s2.toUpperCase()).forEach(value -> System.out.println(value));
}
}

Output:
E
F
G
H


3. Execution


Intermediate operations are not executed until a terminal operation is performed.

Terminal operation is executed as and when it is invoked.

Example:
 StreamString> stream = Stream.of("a", "b", "c", "d");

// till this point no execution will take value
Stream stream1 = stream.sorted().map(s->s.toUpperCase());

// first, all the chained intermediate operations in the pipeline are executed and then the terminal operation executes.
ListString> list = stream1.collect(Collectors.toList());

4. Laziness


Intermediate operations are always lazy which means they will not be executed till a result of processing is required. In simple words, laziness also allows avoiding examining all the data when it is not necessary.

Terminal operations are not lazy i.e. eager except iterator() and spliterator().

5. Number of operations:


A stream pipeline consists of a source followed by zero or more intermediate operations as shown below in the example.

 Intermediate Operations:

filter(Predicate)
peek(Consumer)
distinct()
limit(long n)
skip(long n)
map(Function)
flatMap(Function)
sorted(Comparator)

A stream pipeline must have one and only one terminal operation. Some examples of terminal operations are given below:

 Terminal Operations:

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


6. Short-Circuiting Operation

An intermediate operation is short circuiting in Java, if when presented with infinite input, it may produce a finite stream as a result.

Terminal operations are called as short-circuiting if when presented with infinite input, it may terminate in finite time.

Only one intermediate operation exhibits Short-Circuiting: limit().
There are five terminal operations, which can exhibit Short-Circuiting operation: findFirst(), allMatch(), anyMatch(), findAny(), noneMatch().

Recap: Difference between Intermediate and Terminal Operations in Java


Intermediate OperationsTerminal Operations
Return Typestreamvalue or side-effect
ChainingAllowedNot allowed
ExecutionNot executed until terminal operation is performedExecuted as and when it is invoked
LazinessAlways lazyEager except iterator() and spliterator()
Number of operationsZero or moreExactly one
Short circuiting operation exampleslimit()findFirst(), findAny()


That's all for today. Please mention in the comments if you have any questions related to difference between intermediate and terminal operations in Java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

6 Difference between Intermediate and Terminal Operations in Java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×