AntiPattern

Duplicate Key Merge Missing

to-map-duplicate-key-missing-merge

the Collector does not know what to do with the duplicates in the merge operation — Collectors.toMap. Outcome: if duplicate keys are encountered. | java.lang.IllegalStateException: Duplicate key a (attempted merging values 1 and 1) — I'm trying to convert a list of objects to a Map using Java Streams. Tension: I expected it to just create a map with unique keys, but I forgot that my list has duplicates. | tell it with a merge function what to do in case of a duplicate — To handle it. Tension: or just keep the first one? Outcome: collect(Collectors.toMap( s -> s, String::length, (a1, a2) -> a1 )).