作者:白开水冲清茶 | 来源:互联网 | 2022-12-17 10:12
我有两个数组映射.
Map> map1 = new HashMap<>();
Map> map2 = new HashMap<>();
我想将它们合并到一个新地图中.
如果两个映射中都存在密钥,那么我应该合并数组.
例如:
map1.put("k1", Arrays.asList("a0", "a1"));
map1.put("k2", Arrays.asList("b0", "b1"));
map2.put("k2", Arrays.asList("z1", "z2"));
// Expected output is
Map 3: {k1=[a0, a1], k2=[b0, b1, z1, z2]}
我尝试用流来做到这一点
Map> map3 = Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().stream().collect(Collectors.toList())
));
如果地图中没有相同的键,则此工作.否则,我得到例外
Exception in thread "main" java.lang.IllegalStateException: Duplicate key k2 (attempted merging values [b0, b1] and [z1, z2])
at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:133)
at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:180)
at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.base/java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1751)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at im.djm.Test.main(Test.java:25)
有没有办法用流完成这项任务?
或者我必须迭代地图?
1> Ousmane D...:
在重复键的情况下使用合并功能:
Map> map3 = Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> new ArrayList<>(e.getValue()),
(left, right) -> {left.addAll(right); return left;}
));
请注意,我已经改变 e -> e.getValue().stream().collect(Collectors.toList())
,以new ArrayList<>(e.getValue())
保证我们始终有一个可变的列表,我们可以在合并功能添加进去.
2> Steve11235..:
也许。但是,您更有可能通过使用迭代手动组合条目来使所有事情正确。我不知道是否还有其他人需要处理此代码,但他们可能会对这种易于阅读的方法表示感谢。