Java中的 LongSummaryStatistics getAverage() 方法及示例
Java中 LongSummaryStatistics 类的getAverage()方法用于获取此 LongSummaryStatistics 中记录的平均值。
句法:
public double getAverage()
参数:此方法不接受任何值作为参数。
返回值:此方法返回此 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 average of values is "
+ longSummaryStatistics.getAverage());
}
}
输出:
The average of values is 30.0
参考: https: Java/util/LongSummaryStatistics.html#getAverage()