Stream forEachOrdered(Consumer action) performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order. Stream forEachOrdered(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Syntax :
void forEachOrdered(Consumer<? super T> action) Where, Consumer is a functional interface which is expected to operate via side-effects. and T is the type of stream elements.
Note : This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements.
Example 1 : To print the elements of integer array in original order.
// Java code for forEachOrdered // (Consumer action) in Java 8 import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of Integers List<Integer> list = Arrays.asList(10, 19, 20, 1, 2); // Using forEachOrdered(Consumer action) to // print the elements of stream in encounter order list.stream().forEachOrdered(System.out::println); } } |
10 19 20 1 2
Example 2 : To print the elements of string array in original order.
// Java code for forEachOrdered // (Consumer action) in Java 8 import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a list of Strings List<String> list = Arrays.asList("GFG", "Geeks", "for", "GeeksforGeeks"); // Using forEachOrdered(Consumer action) to // print the elements of stream in encounter order list.stream().forEachOrdered(System.out::println); } } |
GFG Geeks for GeeksforGeeks
Example 3 : To print the characters at index 2 of string array in original order.
// Java code for forEachOrdered // (Consumer action) in Java 8 import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of("GFG", "Geeks", "for", "GeeksforGeeks"); // Using forEachOrdered(Consumer action) stream.flatMap(str-> Stream.of(str.charAt(2))) .forEachOrdered(System.out::println); } } |
G e r e
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- IntStream forEachOrdered() method in Java
- LongStream forEachOrdered() method in Java
- DoubleStream forEachOrdered() method in Java
- Stream.max() method in Java with Examples
- Stream min() method in Java with Examples
- Stream dropWhile() method in Java with examples
- Stream count() method in Java with examples
- OptionalLong stream() method in Java with examples
- Stream generate() method in Java with examples
- OptionalInt stream() method in Java with examples
- Optional stream() method in Java with examples
- BitSet stream() Method in Java with Examples
- Stream skip() method in Java with examples
- Stream ofNullable(T) method in Java with examples
- Stream forEach() method in Java with examples
- OptionalDouble stream() method in Java with examples
- Stream takeWhile() method in Java with examples
- Stream noneMatch() method in Java with examples
- Stream iterate(T,Predicate,UnaryOperator) method in Java with examples
- Difference between Stream.of() and Arrays.stream() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

