1 回答
![?](http://img1.sycdn.imooc.com/5333a1d100010c2602000200-100-100.jpg)
TA贡献1813条经验 获得超2个赞
您可以尝试使用collectingAndThenwith groupingByas downstream as :
private Map<String, List<RelationShip>> groupAndMapRelationships(List<RelationShip> relationShips) {
return relationShips.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(RelationShip::getRelationshipType),
map -> map.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(
e.getValue().size() == 1 ?
e.getKey().getRelationship() : e.getKey().getRelationshipPlural(),
e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
}
我考虑过的最小 POJO 看起来像:
@Getter
class RelationShip {
String personA;
String personB;
RelationshipType relationshipType;
}
@Getter
class RelationshipType {
String relationship;
String relationshipPlural;
}
添加回答
举报