This intermediate operation returns an DoubleStream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
package com.logicbig.example.doublestream;
import java.util.stream.DoubleStream;
public class PeekExample {
public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.peek(d -> System.out.println("peek 1: " + d))
.filter(d -> d > 1.5)
.peek(d -> System.out.println("peek 2: " + d))
.map(d -> d + 10)
.forEach(System.out::println);
}
}
Output
peek 1: 1.0
peek 1: 1.2
peek 1: 2.0
peek 2: 2.0
12.0
peek 1: 2.4
peek 2: 2.4
12.4
peek 1: 3.0
peek 2: 3.0
13.0
For parallel stream:
package com.logicbig.example.doublestream;
import java.util.stream.DoubleStream;
public class PeekExample2 {
public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
ds.parallel()
.peek(d -> System.out.println("peek 1: " + d))
.filter(d -> d > 1.5)
.peek(d -> System.out.println("peek 2: " + d))
.map(d -> d + 10)
.forEach(System.out::println);
}
}
Output
peek 1: 2.0
peek 2: 2.0
12.0
peek 1: 3.0
peek 2: 3.0
13.0
peek 1: 1.0
peek 1: 2.4
peek 1: 1.2
peek 2: 2.4
12.4