public class Test {
public static void main(String[] args) {
List> list = new ArrayList<>();
list.add(new
I believe it's just that type inference is failing, basically - because the reverse()
call gets in the way of the expected argument type of sorted()
and the lambda expression.
You can do it if you specify the type to comparingInt
explicitly:
list.stream()
.sorted(Comparator.<Pair<String, Integer>>comparingInt(p -> p.v).reversed())
.map(p -> p.k)
.forEach(System.out::println);
Or if you just declare the comparer first:
Comparator<Pair<String, Integer>> forward = Comparator.comparingInt(p -> p.v);
list.stream()
.sorted(forward.reversed())
.map(p -> p.k)
.forEach(System.out::println);
It feels to me like there should be a Stream.reverseSorted
so make this sort of thing really easy, but it doesn't look like it exists :(