Java中的 LongSummaryStatistics getSum() 方法及示例
Java中LongSummaryStatistics类的getSum()方法用于获取该LongSummaryStatistics中记录的总和。
句法:
public long getSum()
参数:此方法不接受任何值作为参数。
返回值:此方法返回此 LongSummaryStatistics 中记录的总和。
程序:
// Java program to demonstrate
// the above method
import java.util.*;
public class LongSummaryStatisticsDemo {
public static void main(String[] args)
{
LongSummaryStatistics longSummaryStatistics
= new LongSummaryStatistics();
List list
= Arrays.asList(10, 20, 30, 40, 50);
Iterator iterator = list.listIterator();
while (iterator.hasNext()) {
// Add the integers to the LongSummaryStatistics object
longSummaryStatistics.accept(iterator.next());
}
System.out.println("The sum of values is "
+ longSummaryStatistics.getSum());
}
}
输出:
The sum of values is 150
参考: https: Java/util/LongSummaryStatistics.html#getSum()