Java 8 Collectors.toMap SortedMap

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 … Read more

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

The problem is that topAgents is dynamic – so your ToList() call is dynamic, and so is Select. That has issues that: you can’t use lambda expressions for dynamic calls like this; dynamic calls don’t find extension methods anyway. Fortunately, the operations don’t need to be dynamic just because the element type is dynamic. You … Read more