Stream Intermediate vs Terminal Operation in Java 8 - Java @ Desk

Friday, December 11, 2020

Stream Intermediate vs Terminal Operation in Java 8

Stream Intermediate vs Terminal Operation in Java 8

In Java 8, Stream is used to perform operations in pipeline to either get another Stream or Collection or Object. Stream operations are performed on objects that are returning Stream and not on any other type.

The Stream operation that returns another Stream is an Intermediary Operation. Hence these can be chained together to form a Pipeline. Basic example is, we can perform filter operations on a Stream to remove Null objects from a Collection - ".filter(Objects::nonNull)", then perform another filter operation on returned Stream as per business logic i.e. ".filter(person -> person.getGender().equals(Gender.MALE))". We can have multiple Intermediate Operations as shown above. They are lazily loaded. They are just stored in the memory and executed when the terminal operation is called on the stream.

The Stream operation that returns anything other than Stream i.e. Object or a Collection type is an Terminal Operation. These operations cannot be chained together. We can have only 1 Terminal Operations at the end.

Stream Operations - filter(), map(), limit(), skip()
Intermediate Operations - forEach(), collect(), count(), min(), max(), etc.

IntermediaryTerminalStream.java - In this example, we will learn 2 Intermediate(filter & map) and 2 Terminal(collect & count).


package com.learning;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.learning.model.Gender;
import com.learning.model.Person;

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

		List persons = new ArrayList<>();
		Person personOne = new Person(10, "John", Gender.MALE);
		Person personTwo = new Person(11, "Andy", Gender.MALE);
		Person personThree = new Person(12, "Peter", Gender.MALE);
		Person personFour = new Person(13, "Sarah", Gender.FEMALE);
		Person personFive = new Person(14, "Gillian", Gender.FEMALE);

		persons.add(personOne);
		persons.add(personTwo);
		persons.add(personThree);
		persons.add(personFour);
		persons.add(personFive);

		// Filter is a Intermediate Operation, since it returns a Stream and we can
		// perform
		// Terminal Operations on them.
		Stream filterIntermediateOperation = persons.stream().filter(Objects::nonNull)
				.filter(p -> p.getGender().equals(Gender.MALE));

		// map is a Intermediate Operation, since it returns a Stream and we can perform
		// Terminal Operations on them.
		Stream mapIntermediateOperation = persons.stream().filter(Objects::nonNull)
				.filter(p -> p.getGender().equals(Gender.MALE)).map(Person::getName);

		// collect is a Terminal Operation. No Intermediary operations can be performed,
		// since its returning a List instead of Stream.
		List collectTerminalOperation = persons.stream().filter(Objects::nonNull)
				.filter(p -> p.getGender().equals(Gender.MALE)).collect(Collectors.toList());

		// count is a Terminal Operation. No Intermediary operations can be performed,
		// since its returning a Integer instead of Stream.
		Integer countTerminalOperation = (int) persons.stream().filter(Objects::nonNull)
				.filter(p -> p.getGender().equals(Gender.MALE)).count();

	}
}









No comments:

Post a Comment