通过数组和流的转换来求平均值:
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);
//第二种方式
double average1 = doubleList.stream().collect(Collectors.averagingDouble(Double::doubleValue));
System.out.println("平均值:" + average1);
}
}