I keep hitting this error and I'm not able to make sense of it, because it complains about a value that is present exactly once.
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>
This is the original 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]));
However, the value it complains about exists exactly once. Validation code:
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]));
And the output is: Trouble size: 1
So, there is only one entry that has this value (and, I must add, I'm storing it in the Value side of the Map, not the Key)
System configuration:
So how in the world am I getting that exception? This looks like a JDK bug to me - anybody seeing something I did wrong?
My solution is to replace buggy Collectors.toMap() with Guava's ImmutableMap.toImmutableMap(). Yay!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With