Java Streams - Stream forEachOrdered(Consumer action) example








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;
// www .  j a va2s  .  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.