Java8获取List集合元素最大、最小、总和、平均值

public static void main(String[] args) {
        initEmp();
        int sum = empList.stream().mapToInt(Employee->Employee.getAge()).sum();
        int max = empList.stream().mapToInt(Employee->Employee.getAge()).max().getAsInt();
        int min = empList.stream().mapToInt(Employee->Employee.getAge()).min().getAsInt();
        double avg = empList.stream().mapToInt(Employee->Employee.getAge()).average().getAsDouble();
        System.out.println("最大值:"+max+"\n最小值:"+min+"\n总和:"+sum+"\n平均值:"+avg);
}


版权声明:本文为wenchangwenliu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。