Java Streams - LongStream peek(LongConsumer action) example








LongStream peek(LongConsumer action) returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

Syntax

peek has the following syntax.

LongStream peek(LongConsumer action)

Example

The following example shows how to use peek.

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.