如何在Java流API中使用"reduce"函数求流的平均值?
How to find the average value of the stream using the "reduce" function in Java Stream API?
我有:
Stream.of(120, 410, 85, 32, 314, 12)
我试过了,但好像不对:
Stream.of(120, 410, 85, 32, 314, 12).reduce((a, b) -> b * b / a);
这是一种更简单的解决方法:
double average = IntStream.of(120, 410, 85, 32, 314, 12)
.average()
.getAsDouble();
System.out.println(average);
输出:
162.16666666666666
我有:
Stream.of(120, 410, 85, 32, 314, 12)
我试过了,但好像不对:
Stream.of(120, 410, 85, 32, 314, 12).reduce((a, b) -> b * b / a);
这是一种更简单的解决方法:
double average = IntStream.of(120, 410, 85, 32, 314, 12)
.average()
.getAsDouble();
System.out.println(average);
输出:
162.16666666666666