Java Stream forEachOrdered()

Java Stream forEachOrdered(action) method is used to iterate over all the elements of the given Stream and to perform an Consumer action on the each element of the Stream, in the encounter order of the Stream if the Stream has a defined encounter order.

1. Stream forEachOrdered() Method

1.1. Method Syntax

The forEachOrdered() method syntax is as follows:

void forEachOrdered(Consumer<? super T> action)

Consumer is a functional interface and action represents the a non-interfering action to be performed on each element in the stream.

1.2. Description

  • The forEachOrdered() method is a terminal operation. It means that it does not return an output of type Stream.
  • After forEachOrdered() is performed, the stream pipeline is considered consumed, and can no longer be used.
  • If we need to traverse the same data source again, we must return to the data source to get a new stream.
  • Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
  • Performing the action for one element happens-before performing the action for subsequent elements. But for any given element, the action may be performed in whatever Thread the library chooses.

2. Stream forEach() vs forEachOrdered()

The behavior of forEach() operation is explicitly non-deterministic. For parallel streams, forEach() operation does not guarantee to respect the encounter order of the Stream.

While the forEachOrdered() operation respects the the encounter order of the Stream if the stream has a defined encounter order. This behavior is true for parallel streams as well as sequential streams.

We may lose the benefits of parallelism if we use forEachOrdered() with parallel Streams.

Example 1: Java program to iterate over Stream of Integers and verify encounter ordering

List<Integer> list = Arrays.asList(2, 4, 6, 8, 10);

list.stream().parallel().forEach( System.out::println );		//1

list.stream().parallel().forEachOrdered( System.out::println );	//2

In the above example, both statements guarantee that output will be 2, 4, 6, 8, 10.

//forEach()
6
10
8
4
2

//forEachOrdered()
2
4
6
8
10

3. Stream forEachOrdered() Example

Example 2: Java program to iterate over Stream of Integers and to print into the Console

List<Integer> list = Arrays.asList(2, 4, 6, 8, 10); 

list.stream()
    .forEachOrdered( System.out::println );

Program output.

2
4
6
8
10

Example 3: Java program to iterate over Stream of Integers in reverse order

List<Integer> list = Arrays.asList(2, 4, 6, 8, 10); 

list.stream()
	.sorted(Comparator.reverseOrder())
	.forEachOrdered(System.out::println);

Program output.

10
8
6
4
2

Drop me your questions related to Stream forEachOrdered() method in Java Stream API.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.