I have
Map<String,LongAdder>
and I want to sort by the value the best way in streams. This is harder than with Long because LongAdder does not implement Comparable so I have to use longValue (or intValue if using subtraction to make the Comparator).
I know I can use
m.entrySet().stream().sorted((a, b) -> b.getValue().intValue() - a.getValue().intValue())
but I actually want to also sort secondarily on the key (String). I'm also reversing the sort.
I want to do
m.entrySet().stream().sorted(
Comparator.comparing((a, b) -> b.getValue().intValue() - a.getValue().intValue()))
so that I can chain more comparators afterward with thenComparing()
The exception is
Lambda expression's signature does not match the signature of the functional interface method apply(T)
But even declaring a standalone Comparator this does not work:
Comparator<Map.Entry<String,LongAdder>> byCount = Comparator.comparing((a,b) ->
(b.getValue().intValue() - a.getValue().intValue()));
Lambda expression's signature does not match the signature of the functional interface method apply(T)
I can't use functional reference "::" because it's too many parts: Map.Entry.getValue().intValue().