Java Streams - LongStream forEach(LongConsumer action) example








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

Syntax

forEach has the following syntax.

void forEach(LongConsumer action)

Example

The following example shows how to use forEach.

import java.util.stream.LongStream;

public class Main {
  public static void main(String[] args) {
    LongStream b = LongStream.of(1L, 2L, Long.MAX_VALUE, Long.MIN_VALUE);

    b.forEach(System.out::println);
  }
}

The code above generates the following result.