1 回答
TA贡献1884条经验 获得超4个赞
您似乎正在寻找的是一个条件groupingBy:
Map<String, List<LeadChannel>> finalMap = groups
.stream()
.collect(Collectors.groupingBy(channel ->
channel.getResolution().getId().equals(RESOLVED_STATUS) ?
"unresolved" : "resolved"));
或者在多个管道中,了解如何对数据进行分区,然后根据问题中共享的条件进一步映射它:
Map<Boolean, List<LeadChannel>> mappedUp = groups
.stream()
.collect(Collectors.partitioningBy(channel ->
channel.getResolution().getId().equals(RESOLVED_STATUS)));
Map<String, List<LeadChannel>> finalMap = mappedUp.entrySet().stream()
// in a similar manner you can map your current map's entries as well
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey() ? "resolved" : "unresolved", e.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
或者正如 Holger 建议的那样,最好使用 lambda 直接收集为
Map<String, List<LeadChannel>> finalMap = mappedUp.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey()? "resolved": "unresolved", Map.Entry::getValue))
添加回答
举报