The Stream forEachOrdered() method is used to iterate over all the elements of the given Stream and to perform a Consumer action on 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)
Here Consumer is a functional interface and action represents 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 typeStream. - 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
actionfor 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-beforeperforming theactionfor subsequent elements. But for any given element, theactionmay be performed in whateverThreadthe 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 encounter order of the stream if the stream has a defined encounter order. This behavior is also true for parallel streams as well as sequential streams.
We may lose the benefits of parallelism if we use
forEachOrdered()with parallel Streams.
Let us understand with a Java program that iterates over a Stream of Integers and verifies encounter order.
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
Now, lets compare the output of both statements
//forEach()
6
10
8
4
2
//forEachOrdered()
2
4
6
8
10
3. Stream forEachOrdered() Examples
Example 1: 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 2: 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 the Stream forEachOrdered() method in Java Stream API.
Happy Learning !!
Very well explained!! But forEachOrdered is well suited only with parallel processing becoz its performance is slow as compared to forEach(). Is there any specific scenario by when we should forEachOrdered() instead of parallel processing?
Please put you thoughts and suggestions.
The forEachOrdered() is only helpful when we want to maintain ordering, such as debug or log the processing steps to ensure that the output reflects the order of elements in the source stream. Or, business requirements that need for ordered processing, even if it comes at the cost of performance. Otherwise there is no particular benefit.