list.stream方法的统计操作可以使用collect(Collectors.summarizingInt())方法来进行统计,可以统计List中元素的个数、总和、平均值、最大值和最小值等信息。示例如下:
Listnumbers = Arrays.asList(1, 2, 3, 4, 5); IntSummaryStatistics stats = numbers.stream() .collect(Collectors.summarizingInt(Integer::intValue)); System.out.println("Count: " + stats.getCount()); System.out.println("Sum: " + stats.getSum()); System.out.println("Average: " + stats.getAverage()); System.out.println("Max: " + stats.getMax()); System.out.println("Min: " + stats.getMin());
输出结果为:
Count: 5 Sum: 15 Average: 3.0 Max: 5 Min: 1