Close

Java 8 Streams - IntStream.peek Examples

[Last Updated: Oct 30, 2025]

Java 8 Streams Java Java API 


Interface:

java.util.stream.IntStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.IntStreamIntStreamLogicBig

Method:

IntStream peek(IntConsumer action)

This intermediate operation returns an IntStream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

Examples


package com.logicbig.example.intstream;

import java.util.stream.IntStream;

public class PeekExample {

public static void main(String... args) {
IntStream intStream = IntStream.range(1, 5);
int sum = intStream.peek(System.out::println)
.sum();
System.out.println(sum);
}
}

Output

1
2
3
4
10




See Also