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
-
@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.dimitris93– dimitris932015年11月26日 20:15:47 +00:00Commented Nov 26, 2015 at 20:15
1 Answer 1
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
-
@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– Peter Lawrey2015年11月27日 15:36:00 +00:00Commented Nov 27, 2015 at 15:36
lang-java