java8 实现分组求和,平均等数据统计

java8 实现分组聚合,平均等数据统计

    • 写在前面
    • 一、分组实现
      • 1.1、简单分组
      • 1.2、集合分组
      • 1.3、分组求和
      • 1.4、分组求平均

写在前面

一、分组实现

1.1、简单分组

Map<String, Long> departmentNames = departmentDbs.stream()
                .collect(Collectors.toMap(
                			Department::getDepartmentName, 
                			Department::getDepartmentId)
                		);

1.2、集合分组

       /**
     * 分组函数使用
     * 这个示例返回,分组后的集合对象
     */
    @Test
    public void t12_2() {
        List<Person> roster = Arrays.asList(
                new Person("as", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zx", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zc", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zv", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zb", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zn", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zm", LocalDate.now(), Person.Sex.MALE,"Test@",12)
        );

        Map<Person.Sex, List<Person>> collect = roster.stream()
                .collect(
                        Collectors.groupingBy(Person::getGender)
                );
        collect.forEach((k,v) -> System.out.println(k + " : " + v));
    }

1.3、分组求和

/**
     * 分组函数并求和
     * MALE : 36
     * FEMALE : 48
     */
    @Test
    public void t14() {
        List<Person> roster = Arrays.asList(
                new Person("as", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zx", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zc", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zv", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zb", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zn", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zm", LocalDate.now(), Person.Sex.MALE,"Test@",12)
        );

        Map<Person.Sex, Integer> totalAgeByGender =
                roster.stream()
                        .collect(
                                Collectors.groupingBy(
                                        Person::getGender,
                                        Collectors.reducing(
                                                0,
                                                Person::getAge,
                                                Integer::sum))
                        );
        totalAgeByGender.forEach((k,v) -> System.out.println(k + " : " + v));
    }

1.4、分组求平均

/**
     * 分组求平均
     * MALE : 12.0
     * FEMALE : 12.0
     */
    @Test
    public void t15() {
        List<Person> roster = Arrays.asList(
                new Person("as", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zx", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zc", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zv", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zb", LocalDate.now(), Person.Sex.MALE,"Test@",12),
                new Person("zn", LocalDate.now(), Person.Sex.FEMALE,"Test@",12),
                new Person("zm", LocalDate.now(), Person.Sex.MALE,"Test@",12)
        );

        Map<Person.Sex, Double> averageAgeByGender = roster.stream()
                .collect(
                        Collectors.groupingBy(
                                Person::getGender,
                                Collectors.averagingInt(Person::getAge)));
        averageAgeByGender.forEach((k,v) -> System.out.println(k + " : " + v));
    }

你可能感兴趣的:(Java基础)