Java Streams - IntStream forEach(IntConsumer action) example








IntStream forEach(IntConsumer action) performs an action for each element of this stream. This is a terminal operation.

Syntax

forEach has the following syntax.

void forEach(IntConsumer action)

Example

The following example shows how to use forEach.

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(1);
    i.forEach(System.out::println);
  }
}

The code above generates the following result.