Part 10 – Intermediate and Terminal Operations of Stream in Java 8

Intermediate and Terminal Operations of Stream in Java 8

What have we learned so far

1. Functional Interface And Default Methods In Java 8 –
PART 1 : FUNCTIONAL INTERFACE AND DEFAULT METHODS IN JAVA 8

2. Lambda Expression in Java 8
PART 2 – LAMBDA EXPRESSION IN JAVA 8

3. Method Reference in Java 8
PART 3 – METHOD REFERENCE IN JAVA 8

4. Optional in Java 8
PART 4 – OPTIONAL IN JAVA 8

5. filter(), findAny() in Java 8
PART 5 – FILTER(), FINDANY() IN JAVA 8

6. map() vs flatMap() in Java 8
PART 6 – MAP() VS FLATMAP() IN JAVA 8

7. Stream.peek() in Java 8
PART 7 – STREAM.PEEK() IN JAVA 8

8. Short circuiting operations of Streams in Java 8
PART 8 – SHORT CIRCUITING OPERATIONS OF STREAMS IN JAVA 8

9. Advance Collectors in Java 8
PART 9 – ADVANCE COLLECTORS IN JAVA 8

What is Intermediate and Terminal Operations of Stream in Java 8?

inter

Lets understand this diagram


Stream
We can create stream from object with the help of Stream.of() method.

Intermediate Operations in Java 8

Intermediate operation will transform a stream into another stream, they are composed forming a Pipeline of Stream execution and will not be able to execute until some terminal operation is invoked. Intermediate Operations are lazy, so they don’t get executed until     the actual processing is required. Every Intermediate Operation will return the new Stream and traversal of each Stream does not begin until the terminal operation of the pipeline is executed.

Lets see how intermediate operations are lazy ?

We have a map() function in which we are printing the current student name. These names will only be printed if we apply a terminal operator to it. In below example we have applied the collect(terminal operator) and the map() prints the student names after the thread comes into running state. This is how intermediate operations works.

private static void lazyIntermediateOperations(List<Student> students) throws InterruptedException {
 System.out.println("######## Executing lazyIntermediateOperations() : ######## ");
 Stream<String> studentStream = students.stream()
            .map(student -> {
           System.out.printf("In Map : %sn", student.getName());
           return student.getName().toUpperCase();
      });
 
 System.out.println("After map statement");
 Thread.sleep(5000);
 System.out.println("Thread is in Running state now");
 studentStream.collect(Collectors.toList());
 System.out.println("######## Ending the execution of lazyIntermediateOperations() ######## ");
}

Output
######## Executing lazyIntermediateOperations() : ########
After map statement
Thread is in Running state now
In Map : Saurabh
In Map : Robert
In Map : John
In Map : Roman
In Map : Randy
######## Ending the execution of lazyIntermediateOperations() ########

Intermediate Operations available in Java 8

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

Terminal Operations in Java 8

Terminal operations produces a non-stream (cannot be chained) result such as primitive value, a collection or no value at all.
Terminal Operations available in Java 8 :
forEach
toArray
reduce
collect
min
max
count
Short Circuiting Terminal Operations available in Java 8 :
anyMatch
allMatch
noneMatch
findFirst
findAny
Last 5 are short-circuiting terminal operations.

Difference between Intermediate and Terminal Operations in Java 8Intermediate vs Terminal Operations in Java 8

Sr.  No. 
Intermediate Operations
Terminal Operations
1
They produce stream
They produce result such as primitive value, object or collection
2
They are lazy and they only gets executed when any terminal operation is performed.
They are executes immediately and depends on intermediate
operations to get the result.
3
We can chain multiple Intermediate Operations like
.filter(some condition)
.map(some function)
.flatMap(some function)
We cannot chain multiple terminal operations and each stream
can have only single terminal operation at a time.
Source Code
Download source code of Java 8 features from below git repository :
java-8-features