使用自定义收集器的Java8分组?

使用自定义收集器的Java8分组?,java,lambda,java-8,java-stream,collectors,Java,Lambda,Java 8,Java Stream,Collectors,我有以下课程 class Person { String name; LocalDate birthday; Sex gender; String emailAddress; public int getAge() { return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears(); } public String getName() {

我有以下课程

class Person {

    String name;
    LocalDate birthday;
    Sex gender;
    String emailAddress;

    public int getAge() {
        return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears();
    }

    public String getName() {
        return name;
    }
}
我希望能够按年龄分组,然后收集人名列表,而不是人名对象本身;所有这些都在一个漂亮的lamba表达式中

为了简化所有这些,我将链接我当前的解决方案,该解决方案按年龄存储分组结果,然后对其进行迭代以收集名称

ArrayList<OtherPerson> members = new ArrayList<>();

members.add(new OtherPerson("Fred", IsoChronology.INSTANCE.date(1980, 6, 20), OtherPerson.Sex.MALE, "fred@example.com"));
members.add(new OtherPerson("Jane", IsoChronology.INSTANCE.date(1990, 7, 15), OtherPerson.Sex.FEMALE, "jane@example.com"));
members.add(new OtherPerson("Mark", IsoChronology.INSTANCE.date(1990, 7, 15), OtherPerson.Sex.MALE, "mark@example.com"));
members.add(new OtherPerson("George", IsoChronology.INSTANCE.date(1991, 8, 13), OtherPerson.Sex.MALE, "george@example.com"));
members.add(new OtherPerson("Bob", IsoChronology.INSTANCE.date(2000, 9, 12), OtherPerson.Sex.MALE, "bob@example.com"));

Map<Integer, List<Person>> collect = members.stream().collect(groupingBy(Person::getAge));

Map<Integer, List<String>> result = new HashMap<>();

collect.keySet().forEach(key -> {
            result.put(key, collect.get(key).stream().map(Person::getName).collect(toList()));
});
ArrayList成员=新的ArrayList();
添加(新的OtherPerson(“Fred”),等时学。实例。日期(1980年6月20日),OtherPerson.Sex.MALE,“fred@example.com"));
添加(新的OtherPerson(“Jane”),等时学。实例。日期(1990年7月15日),OtherPerson.Sex.femal,“jane@example.com"));
添加(新的OtherPerson(“标记”),等时学。实例。日期(1990年7月15日),OtherPerson.Sex.MALE,“mark@example.com"));
成员。添加(新的OtherPerson(“乔治”),等时学。实例。日期(1991年8月13日),OtherPerson.Sex.MALE,“george@example.com"));
添加(新的OtherPerson(“Bob”),等时学。实例。日期(2000年9月12日),OtherPerson.Sex.MALE,“bob@example.com"));
Map collect=members.stream().collect(groupingBy(Person::getAge));
映射结果=新的HashMap();
collect.keySet().forEach(键->{
result.put(key,collect.get(key).stream().map(Person::getName).collect(toList());
});


不理想,为了学习,我希望有一个更优雅、更高效的解决方案。

您可以使用
映射
收集器
人的列表映射到人名列表:

Map<Integer, List<String>> collect = 
    members.stream()
           .collect(Collectors.groupingBy(Person::getAge,
                                          Collectors.mapping(Person::getName, Collectors.toList())));
Map collect=
成员:stream()
.collect(收集器).groupingBy(个人::getAge,
Collectors.mapping(Person::getName,Collectors.toList());

您可以使用
映射
收集器
人员列表
映射到人员姓名列表:

Map<Integer, List<String>> collect = 
    members.stream()
           .collect(Collectors.groupingBy(Person::getAge,
                                          Collectors.mapping(Person::getName, Collectors.toList())));
Map collect=
成员:stream()
.collect(收集器).groupingBy(个人::getAge,
Collectors.mapping(Person::getName,Collectors.toList());

将流与分组时,可以使用自定义
收集器对值指定缩减操作。在这里,我们需要使用,它接受一个函数(映射是什么)和一个收集器(如何收集映射值)。在这种情况下,映射是
Person::getName
,即返回人名的方法引用,我们将其收集到
列表中

Map<Integer, List<String>> collect = 
    members.stream()
           .collect(Collectors.groupingBy(
               Person::getAge,
               Collectors.mapping(Person::getName, Collectors.toList()))
           );
Map collect=
成员:stream()
.collect(收集器.groupingBy(
人物:getAge,
Collectors.mapping(Person::getName,Collectors.toList())
);

将流与分组时,可以使用自定义
收集器对值指定缩减操作。在这里,我们需要使用,它接受一个函数(映射是什么)和一个收集器(如何收集映射值)。在这种情况下,映射是
Person::getName
,即返回人名的方法引用,我们将其收集到
列表中

Map<Integer, List<String>> collect = 
    members.stream()
           .collect(Collectors.groupingBy(
               Person::getAge,
               Collectors.mapping(Person::getName, Collectors.toList()))
           );
Map collect=
成员:stream()
.collect(收集器.groupingBy(
人物:getAge,
Collectors.mapping(Person::getName,Collectors.toList())
);

您还可以使用Collectors.toMap并为键、值和合并函数(如果有)提供映射

Map-ageNameMap=
成员:stream()
.collect(collector.toMap)(
person->person.getAge(),
person->person.getName(),(pName1,pName2)->pName1+“|”+pName2)
);

您还可以使用Collectors.toMap并为键、值和合并函数(如果有)提供映射

Map-ageNameMap=
成员:stream()
.collect(collector.toMap)(
person->person.getAge(),
person->person.getName(),(pName1,pName2)->pName1+“|”+pName2)
);

Ops,这很简单,嗯!干杯哦,这很容易啊!干杯