jshell> Stream.of("A quick brown fox jumps over the lazy dog.".split(" ")).collect(Collectors.toMap(s->s.length(),s->s)
...> )
| java.lang.IllegalStateException thrown: Duplicate key 5 (attempted merging values quick and brown)
| at Collectors.duplicateKeyException (Collectors.java:131)
| at Collectors.lambda$uniqKeysMapAccumulator$1 (Collectors.java:178)
| at ReduceOps$3ReducingSink.accept (ReduceOps.java:169)
| at Spliterators$ArraySpliterator.forEachRemaining (Spliterators.java:948)
| at AbstractPipeline.copyInto (AbstractPipeline.java:484)
| at AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:474)
| at ReduceOps$ReduceOp.evaluateSequential (ReduceOps.java:913)
| at AbstractPipeline.evaluate (AbstractPipeline.java:234)
| at ReferencePipeline.collect (ReferencePipeline.java:511)
| at (#9:1)
jshell> Stream.of("A quick brown fox jumps over the lazy dog.".split(" ")).collect(Collectors.toMap(s->s.length(),s->s,(s1,s2)->s2))
$11 ==> {1=A, 3=the, 4=dog., 5=jumps}
new Map()して.set()でmethod chainで繋いで行くのもありますが、 一番簡単なのは、 new Map([['key1','val1'],['key2','val2'],['key3','val3']])という ようなnested arrayでしょうか。 cf. Map & Set