Java新特性玩转JDK8之summarizing集合统计

DBC 716 0
小例子
    public class Main {
        public static void main(String[] args) throws Exception {
            List<Student> students = Arrays.asList(new Student("广东", 23), new Student("广东", 24),
                    new Student("广东", 23),
                    new Student("北京", 22), new Student("北京", 20),
                    new Student("北京", 20), new Student("海南", 25));


            IntSummaryStatistics summaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge));

            System.out.println("平均值"+summaryStatistics.getAverage());
            System.out.println("人数"+summaryStatistics.getCount());

            System.out.println("最大值"+summaryStatistics.getMax());

            System.out.println("最小值"+summaryStatistics.getMin());
            System.out.println("总和"+summaryStatistics.getSum());





        }
    }


class Student {
    private int age;

    private String province;


    public Student(String province, int age) {
        this.province = province;
        this.age = age;
    }

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }
}
控制台输出Java新特性玩转JDK8之summarizing集合统计插图

发表评论 取消回复
表情 图片 链接 代码

分享