0

I am trying to sort a map like so (first by value (Integer) then by key (String))

public static Map<String, Integer> sortMap(Map<String, Integer> map) {
    List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); 
    // thenComparing( ... ) is causing an error
    list.sort(Map.Entry.comparingByValue().thenComparing(Map.Entry.comparingByKey()));   

    //...
}

I am getting the following error:

1

Any idea what I am missing ? This was suggested as an alternative in my previous question, but I can't make it work.

Community
  • 1
  • 1
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • @Reimeus "java -version" in cmd gives me "1.8.0_51", I am not entirely sure what Java 8 means. My project language level is set to 8. [Here](https://i.gyazo.com/913516e232ec55ab24b364ebccb3c0ea.png) is a screenshot of my intellij idea settings. – dimitris93 Nov 26 '15 at 20:15

1 Answers1

5

Unfortunately type inference is failing here you have to give it the generic types.

list.sort(Map.Entry.<String,Integer>comparingByValue()
        .thenComparing(Map.Entry.comparingByKey()));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @Shiro I find that type inference can mean that an error can be a surprisingly long way from the solution to that error. When Java was more verbose, everything was more specific and you got saner errors close to the cause of the problem. Type inference is great but disembodied error messages are it's dark side. – Peter Lawrey Nov 27 '15 at 15:36