Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between intermediate and terminal operations?

can someone tell me What is the difference between intermediate and terminal operations for Stream?

Stream operations are combined into pipelines to process streams. All operations are either intermediate or terminal ..means?.

like image 342
Amol Raje Avatar asked Dec 07 '17 05:12

Amol Raje


People also ask

What is difference between terminal and intermediate?

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.

What are intermediate and 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.

What is terminal operation?

Terminal Operations means services provided at a port, rail or road terminal or station including cargo handling, storing and delivery of cargo to vessels, trains and vehicles, handling of passengers howsoever and services related thereto; Sample 1Sample 2.

What are intermediate operations in stream?

Stream. Intermediate operators do not execute until a terminal operation is invoked, i.e. they are not executed until a result of processing is actually needed. We will be discussing a few of the important and most frequently used: filter(predicate) Method. sorted() Method.


2 Answers

A Stream supports several operations and these operations are divided into intermediate and terminal operations.

The distinction between this operations is that an intermediate operation is lazy while a terminal operation is not. When you invoke an intermediate operation on a stream, the operation is not executed immediately. It is executed only when a terminal operation is invoked on that stream. In a way, an intermediate operation is memorized and is recalled as soon as a terminal operation is invoked. You can chain multiple intermediate operations and none of them will do anything until you invoke a terminal operation. At that time, all of the intermediate operations that you invoked earlier will be invoked along with the terminal operation.

All intermediate operations return Stream (can be chained), while terminal operations don't. Intermediate Operations are:

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

Terminal operations produces a non-stream (cannot be chained) result such as primitive value, a collection or no value at all.

Terminal Operations are:

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

Last 5 are short-circuiting terminal operations.

like image 185
Alex Mamo Avatar answered Oct 12 '22 18:10

Alex Mamo


As per javadoc:

  • Intermediate operation will transform a stream into another stream, such as map(MapperFn) or filter(Predicate)
  • Terminal operation will produce a result or side-effect, such as count() or forEach(Consumer)

Note that all intermediate operations will NOT be executed without a terminal operation at the end. So the pattern will be:

stream()
    .intemediateOperation1()
    .intemediateOperation2()
    ...
    .intemediateOperationN()
    .terminalOperation();
like image 20
Hoa Nguyen Avatar answered Oct 12 '22 18:10

Hoa Nguyen