Java中的IntStream sum()方法
sum()Java中使用IntStream类的方法返回此流中元素的总和。
语法如下-
int sum()
要使用Java中的IntStream类,请导入以下包-
import java.util.stream.IntStream;
创建IntStream并添加一些元素-
IntStream intStream = IntStream.of(50, 100, 150, 200, 250, 300);
现在,返回上面添加的IntStream中元素的总和-
int sumVal = intStream.sum();
以下是sum()在Java中实现IntStream方法的示例-
示例
import java.util.stream.IntStream;
public class Demo {
public static void main(String[] args) {
IntStream intStream = IntStream.of(50, 100, 150, 200, 250, 300);
int sumVal = intStream.sum();
System.out.println("Sum of elements in the stream = "+sumVal);
}
}输出结果
Sum of elements in the stream = 1050
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿