sum()
Java中DoubleStream类的方法返回此流中元素的总和。
语法如下
double sum()
要在Java中使用DoubleStream类,请导入以下包
import java.util.stream.DoubleStream;
创建DoubleStream并添加一些元素
DoubleStream doubleStream = DoubleStream.of(23.6, 45.3, 59.6, 60.6, 73.6, 84.7, 94.8);
现在,对流中的元素求和
double sum = doubleStream.sum();
以下是sum()
在Java中实现DoubleStream方法的示例
import java.util.*; import java.util.stream.DoubleStream; public class Demo { public static void main(String[] args) { DoubleStream doubleStream = DoubleStream.of(23.6, 45.3, 59.6, 60.6, 73.6, 84.7, 94.8); double sum = doubleStream.sum(); System.out.println("The sum of all the elements of the stream = "+sum); } }
输出结果
The sum of all the elements of the stream = 442.2