Java 8 grouping using custom collector?

后端 未结 3 1163
鱼传尺愫
鱼传尺愫 2020-11-30 11:52

I have the following class.

class Person {

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

    public int getAge() {
             


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 12:09

    You can use a mapping Collector to map the list of Person to a list of person names :

    Map> collect = 
        members.stream()
               .collect(Collectors.groupingBy(Person::getAge,
                                              Collectors.mapping(Person::getName, Collectors.toList())));
    

提交回复
热议问题