How to group elements of a List by elements of another in Java 8

前端 未结 5 1433
伪装坚强ぢ
伪装坚强ぢ 2021-02-16 00:09

I have the following problem: Given these classes,

class Person {
    private String zip;
    ...
    public String getZip(){
        return zip;
    }
}

class          


        
5条回答
  •  面向向阳花
    2021-02-16 00:46

    I suspect the cleanest way to do this -- I'm not quite happy with the other answers posted -- would be

     persons.stream().collect(Collectors.toMap(
        person -> person,
        person -> regions.stream()
           .filter(region -> region.getZipCodes().contains(person.getZip()))
           .collect(Collectors.toList())));
    

提交回复
热议问题