Von*_*ngo 13 java lambda java-8 java-stream
我想在不丢失密钥映射的情况下压扁Map将Integer密钥与列表相关联的密钥String.我很好奇,好像用stream和做这样做是有用的和有用的lambda.
我们从这样的事情开始:
Map<Integer, List<String>> mapFrom = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我们假设mapFrom填充在某处,看起来像:
1: a,b,c
2: d,e,f
etc.
Run Code Online (Sandbox Code Playgroud)
我们还假设列表中的值是唯一的.
现在,我想"展开"它以获得第二张地图:
a: 1
b: 1
c: 1
d: 2
e: 2
f: 2
etc.
Run Code Online (Sandbox Code Playgroud)
我可以这样做(或非常相似,使用foreach):
Map<String, Integer> mapTo = new HashMap<>();
for (Map.Entry<Integer, List<String>> entry: mapFrom.entrySet()) {
for (String s: entry.getValue()) {
mapTo.put(s, entry.getKey());
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们假设我想使用lambda而不是嵌套for循环.我可能会这样做:
Map<String, Integer> mapTo = mapFrom.entrySet().stream().map(e -> {
e.getValue().stream().?
// Here I can iterate on each List,
// but my best try would only give me a flat map for each key,
// that I wouldn't know how to flatten.
}).collect(Collectors.toMap(/*A String value*/,/*An Integer key*/))
Run Code Online (Sandbox Code Playgroud)
我也尝试过flatMap,但我不认为这是正确的方法,因为虽然它有助于我摆脱维度问题,但我在这个过程中失去了关键.
简而言之,我的两个问题是:
streams并lambda实现这一目标吗?Hol*_*ger 18
您需要使用flatMap将值展平为新流,但由于您仍需要将原始密钥收集到a中Map,因此必须映射到包含键和值的临时对象,例如
Map<String, Integer> mapTo = mapFrom.entrySet().stream()
.flatMap(e->e.getValue().stream()
.map(v->new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v)))
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Run Code Online (Sandbox Code Playgroud)
它Map.Entry是不存在的元组类型的替身,任何其他能够容纳两个不同类型的对象的类型就足够了.
另一种不需要这些临时对象的方法是自定义收集器:
Map<String, Integer> mapTo = mapFrom.entrySet().stream().collect(
HashMap::new, (m,e)->e.getValue().forEach(v->m.put(v, e.getKey())), Map::putAll);
Run Code Online (Sandbox Code Playgroud)
这与toMap静默覆盖重复键不同toMap,如果存在重复键,则没有合并函数会抛出异常.基本上,这个自定义收集器是一个并行能力的变种
Map<String, Integer> mapTo = new HashMap<>();
mapFrom.forEach((k, l) -> l.forEach(v -> mapTo.put(v, k)));
Run Code Online (Sandbox Code Playgroud)
但请注意,即使使用非常大的输入映射,此任务也不会受益于并行处理.只有在流管道中存在可以从SMP中受益的额外计算密集任务时,才有可能从并行流中获益.或许,简洁,顺序的Collection API解决方案更可取.
你应该使用flatMap如下:
entrySet.stream()
.flatMap(e -> e.getValue().stream()
.map(s -> new SimpleImmutableEntry(e.getKey(), s)));
Run Code Online (Sandbox Code Playgroud)
SimpleImmutableEntry是一个嵌套类AbstractMap.
| 归档时间: |
|
| 查看次数: |
10423 次 |
| 最近记录: |