Stream forEachOrdered(Consumer action) example
Description
Stream forEachOrdered(Consumer<? super T> action)
performs an action for each element of this stream, in the encounter order of the stream.
Syntax
forEachOrdered
has the following syntax.
void forEachOrdered(Consumer<? super T> action)
Example
The following example shows how to use forEachOrdered
.
import java.util.Arrays;
import java.util.List;
//from w w w. j a v a 2s .c o m
public class Main {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("2","1","3","4");
stringList.stream()
.forEachOrdered(System.out::println);
}
}
The code above generates the following result.