I have a list of map entries
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
and I want to sort it by values according to this answer, but in reverse order. When I do
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
everything works fine. But when I try to shorten the above two lines to
entries.sort(Entry.comparingByValue().reversed());
the compiler returns
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
This seems strange because the implementation of Comparator's reversed()
default method simply transfers it to Collections.reverseOrder()
and I can change the above line to
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
to work. But what is the correct type of Entry.comparingByValue().reversed()
?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
doesn't seem to work. Omitting the type parameter Entry<String, Integer>
works, but this results in "unchecked method invocation" warning from the compiler later.