1

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.

asked Nov 26, 2015 at 20:10
1
  • @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 is a screenshot of my intellij idea settings. Commented Nov 26, 2015 at 20:15

1 Answer 1

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()));
answered Nov 26, 2015 at 20:24
1
  • @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. Commented Nov 27, 2015 at 15:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.