I don’t think you can get much better than this:
.collect(Collectors.toMap(keyMapper, valueMapper,
(v1,v2) ->{ throw new RuntimeException(String.format("Duplicate key for values %s and %s", v1, v2));},
TreeMap::new));
where the throw
lambda is the same as throwingMerger()
but I can’t directly call that since it’s package private (you can of course always make your own static method for that like throwingMerger()
is. )