Generic type inference not working with method chaining?
Why ?
Because the type inference of generics types has not been expanded to chained invocation.
From the java tutorial on generics type inference:
The notion of what is a target type has been expanded to include method arguments.
That is why this code:
f(Map.empty());
compiles.
But this code doesn't because this is a chained invocation:
f(Map.empty().put(1,"A").put(2,"B"));
You can also find a small paragraph in the JSR-000335 Lambda Expressions for the JavaTM Programming Language Final Release for Evaluation (specifically part D):
There has been some interest in allowing inference to "chain": in a().b(), passing type information from the invocation of b to the invocation of a. This adds another dimension to the complexity of the inference algorithm, as partial information has to pass in both directions; it only works when the erasure of the return type of a() is fixed for all instantiations (e.g. List). This feature would not fit very well into the poly expression model, since the target type cannot be easily derived; but perhaps with additional enhancements it could be added in the future.
So maybe in Java 9.
What is the type of Map.Entry.comparingByValue().reversed()? - Just to answer this question simpler.
unSortedMap.entrySet().stream()
.filter(e -> e.getValue() > 1)
.sorted(Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));