Java 8 Streams Java Java API
java.util.stream.DoubleStream
DoubleStream peek(DoubleConsumer action)
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); }}
peek 1: 1.0peek 1: 1.2peek 1: 2.0peek 2: 2.012.0peek 1: 2.4peek 2: 2.412.4peek 1: 3.0peek 2: 3.013.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); }}
peek 1: 1.2peek 1: 2.4peek 1: 1.0peek 1: 2.0peek 1: 3.0peek 2: 2.0peek 2: 2.412.412.0peek 2: 3.013.0