在 Java8 中流式传输带有嵌套对象的地图
Streaming a Map with a nested object in Java8
我有一张格式为
的地图
Map<Object1, List<Object2>> dependencies;
。有人可以建议使用 Java Streams 将其转换为 Map> 的简洁方法是什么?下面列出了使用流进行失败的尝试。
class Object2 {
List<Object3> subDependencies;
}
//What I was trying to do
Map<Object1, List<Object3>> results = dependencies.entrySet().stream().flatMap
(entry -> entry.getValue().stream()
.collect(Collectors.toMap(entry.getKey(), Object2::getsubDependecies));
试试这个:
Map<Object1, List<Object3>> results =
dependencies.entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
e -> e.getValue()
.stream()
.map(Object2::getSubDependecies)
.flatMap(List::stream)
.collect(toList())));
我有一张格式为
的地图Map<Object1, List<Object2>> dependencies;
。有人可以建议使用 Java Streams 将其转换为 Map
class Object2 {
List<Object3> subDependencies;
}
//What I was trying to do
Map<Object1, List<Object3>> results = dependencies.entrySet().stream().flatMap
(entry -> entry.getValue().stream()
.collect(Collectors.toMap(entry.getKey(), Object2::getsubDependecies));
试试这个:
Map<Object1, List<Object3>> results =
dependencies.entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
e -> e.getValue()
.stream()
.map(Object2::getSubDependecies)
.flatMap(List::stream)
.collect(toList())));