Sorting Descending order: Java Map

You should use new TreeMap<>(Collections.reverseOrder());.

Map<String, Integer> newMap = new TreeMap<>(Collections.reverseOrder());
newMap.putAll(myMap);

or to reverse an existing comparator like the value-comparator Collections.reverseOrder(comparator). It works like your approach swapping the two objects before invoking compare/compareTo.

Leave a Comment

tech