2

This statement...:

singletonMap("key", "value")
    .entrySet()
    .stream()
    .sorted(Comparator.comparing(Map.Entry::getValue))
    .forEach(System.out::println);

prints all map entries sorted by the value in the map. While pointless for a map with only a single entry, it compiles. However if I want the reverse and I try:

singletonMap("key", "value")
    .entrySet()
    .stream()
    .sorted(Comparator.comparing(Map.Entry::getValue).reversed())
    .forEach(System.out::println);

I get the following compilation error:

Error:(100, 85) java: incompatible types: cannot infer type-variable(s) T,U
    (argument mismatch; invalid method reference
      method getValue in interface java.util.Map.Entry<K,V> cannot be applied to given types
        required: no arguments
        found: java.lang.Object
        reason: actual and formal argument lists differ in length)
Error:(100, 89) java: invalid method reference
  non-static method getValue() cannot be referenced from a static context

What happend here that the .reversed() changed so much?

yankee
  • 38,872
  • 15
  • 103
  • 162

0 Answers0