使用stream求数组中的平均值

通过数组和流的转换来求平均值:

public class DoubleStream {
    public static void main(String[] args) {
        List<Double> doubleList = new ArrayList<>();
        doubleList.add(1.3);
        doubleList.add(2.2);
        doubleList.add(3.2);
        doubleList.add(4.3);
        doubleList.add(5.2);
        OptionalDouble average = Arrays.stream(doubleList.toArray(new Double[doubleList.size()])).mapToDouble(Number::doubleValue).average();
        System.out.println("平均值:"+average);

    }
}

 

上一篇:0637. Average of Levels in Binary Tree (E)


下一篇:【春节补档】11/30 模拟赛 T3 warfare