我想转换 List<Map<Integer, List<Object>>>
到 Map<Integer, List<Object>>
与 lambda 但我完全迷失了,到目前为止我得到了这个;
public Map<Integer, List<Object>> getMapofList(List<Map<Integer, List<Object>>> listMaps) {
return listMaps
.stream()
.filter(listMap -> listMap //Obtained Map
.entrySet()
.stream()
.filter(integerListEntry -> nonNull(integerListEntry.getValue())) //Check if there is a List<Object> not null
. //Not sure how to continue to finally just return a Map<Integer, List<Object>>
}
任何提示或帮助将不胜感激!
最佳答案
一种解决方案是:
return listMaps
.stream()
.flatMap(e -> e.entrySet()
.stream())
.filter(e -> e.getValue() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
或者如果可能有重复的键,那么您可以使用 groupingBy
收集器,如下所示:
return listMaps
.stream()
.flatMap(e -> e.entrySet()
.stream())
.filter(e -> e.getValue() != null)
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
关于java - 列表列表到 MapList Java 8 Lambda 的映射列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48329900/