gpt4 book ai didi

java - 收集器.toMap IllegalStateException : Duplicate key when no duplication is possible

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:15:41 27 4
gpt4 key购买 nike

我一直遇到这个错误,但我无法理解它,因为它提示一个值恰好出现一次

Exception in thread "main" java.lang.IllegalStateException: Duplicate key wp-admin/admin-ajax.php#13236
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1245)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at <my code>

这是原始代码:

reader.lines().filter(line -> line.startsWith(TAG_MAPPED_NODE)).map(line -> {
final String[] splitted = line.split(" ");
if (splitted.length != 3)
throw new IllegalStateException("Unexpected line: " + line);
return splitted;(splitted[1],splitted[2]);
}).collect(Collectors.toMap(t -> t[1], t -> t[2]));

但是,它提示的值只存在一次。验证码:

List<String> usefulLines =
reader.lines().filter(line -> line.startsWith(TAG_MAPPED_NODE)).collect(Collectors.toList());
List<String> trouble =
usefulLines.stream().filter(line -> line.contains("wp-admin/admin-ajax.php#13236")).collect(Collectors.toList());
System.out.println("Trouble size: " + trouble.size());
return usefulLines.stream().map(line -> {
final String[] splitted = line.split(" ");
if (splitted.length != 3)
throw new IllegalStateException("Unexpected line: " + line);
return splitted;
}).collect(Collectors.toMap(t -> t[1], t -> t[2]));

输出是:Trouble size: 1

因此,只有一个条目具有此值(而且,我必须补充一点,我将其存储在 Map 的值端,而不是键)

系统配置:

  • Java版本:openjdk版本“1.8.0_72”
  • IDE:IntelliJ IDEA 2016.1
  • 操作系统:Fedora 22 64 位

那么我到底是怎么得到这个异常(exception)的呢?对我来说这看起来像是一个 JDK 错误 - 有人看到我做错了什么吗?

最佳答案

我的解决方案是用 Guava 的 ImmutableMap.toImmutableMap() 替换有问题的 Collectors.toMap() .耶!

关于java - 收集器.toMap IllegalStateException : Duplicate key when no duplication is possible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36068934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com