This terminal operation performs an LongConsumer action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
package com.logicbig.example.longstream;
import java.util.stream.LongStream;
public class ForEachOrderedExample {
public static void main(String... args) {
System.out.println("-- sequential --");
LongStream stream1 = LongStream.range(1, 5);
stream1.forEachOrdered(System.out::println);
System.out.println("-- parallel --");
LongStream stream2 = LongStream.range(1, 5);
stream2.parallel()
.forEachOrdered(System.out::println);
}
}
Output
-- sequential --
1
2
3
4
-- parallel --
1
2
3
4