I'm currently sorting the map by value, but I couldn't think on how I would have it sorted by key for the cases that I have the same value.
Currently it works like this:
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
return e2.getValue().compareTo(e1.getValue());
}
});
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
My output is something like this: (AA,10), (CC,5), (BB,5)
I'm trying to achieve this: (AA,10), (BB,5), (CC,5)