有时候我们会遇到,求一个数组中,一个int字段的和为多少的需求,代码如下:
Lis<Student> list = new ArrayList<>();
int sum = list.stream().mapToInt(Student::getNum).sum();
有点意思吧,还有更多呢
IntSummaryStatistics statistics = list.stream().mapToInt((student) -> student.getNum()).summaryStatistics();
System.out.printIn("总和:"+statistics.getSum());
System.out.printIn("平均数:"+statistics.getAverage());
System.out.printIn("总个数:"+statistics.getCount());
System.out.printIn("最大值:"+statistics.getMax());
System.out.printIn("最小值:"+statistics.getMin());