I have a Map consisting of a String and a corresponding List. The lists have different sizes:
Map<String, List<String>> map = new LinkedHashMap<>();
List<String> l1 = new ArrayList<>();
List<String> l2 = new ArrayList<>();
List<String> l3 = new ArrayList<>();
l1.add("Str1");
l1.add("Str2");
l2.add("Str1");
l2.add("Str2");
l2.add("Str3");
l3.add("Str1");
map.put("Category A", l1);
map.put("Category B", l2);
map.put("Category C", l3);
I want to sort the entries of the Map by the size of the Lists, in descending order. So in my example the final result should be:
Category B: Str1, Str2, Str3
Category A: Str1, Str2
Category C: Str1
I tried:
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
entry.getValue().sort(new Comparator<String>() {
public int compare(ArrayList a1, ArrayList a2) {
return a2.size() - a1.size();
}
});
But I get:
java: <anonymous com.app.class> is not abstract and does not override abstract method compare(java.lang.String,java.lang.String) in java.util.Comparator