语法如下
DoubleStream peek(DoubleConsumer action)
在这里,DoubleConsumer是一个接受单个双值参数并且不返回结果的操作。
要在Java中使用DoubleStream类,请导入以下包
import java.util.stream.DoubleStream;
以下是peek()
在Java中实现DoubleStream方法的示例
import java.util.*; import java.util.stream.DoubleStream; public class Demo { public static void main(String[] args) { DoubleStream doubleStream = DoubleStream.of(28.7, 35.6, 48.3, 69.8, 75.8, 80.5, 90.8); System.out.println("Elements in the stream..."); long num = doubleStream.peek(System.out::println).count(); System.out.println("Number of elements in the stream = " + num); } }
输出结果
Elements in the stream... 28.7 35.6 48.3 69.8 75.8 80.5 90.8 Number of elements in the stream = 7