Java SE API

Stream.forEachOrdered()

Executes terminal actions in encounter order if present; does not guarantee worker identity. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
void forEachOrdered(Consumer<? super T> action)

Parameters & Attributes

NameTypeDescription
actionConsumer<? super T>Action for each element

Return Value

void

No returned value; performs the ordered action.

Working Example

Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main {    public static void main(String[] args) {        var result = Stream.of("a", "b", "c").parallel().collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);        System.out.println(result.toString());        IntStream.rangeClosed(1, 3).parallel().forEachOrdered(System.out::println);    }}
Java Reference