In this article, we will discuss how to sort TreeMap entries by its Keys in descending order
1. Collections.reverseOrder()
- Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the
Comparable
interface - The natural ordering is the ordering imposed by the objects’ own
compareTo
method - Arrays.sort(a, Collections.reverseOrder()); –> sorts the array in reverse-lexicographic (alphabetical) order
- The returned comparator is serializable
- Method signature :- public static <T> Comparator<T> reverseOrder()
1.1 Sorting TreeMap by its Keys in Descending order
- A TreeMap contains 7 entries with Keys being in String type
- TreeMap stores entries in ascending-order of keys
- Desceding-order sorting of Map Keys :- create another TreeMap object passing comparator (i.e.; Collections.reverseOrder()) as constrcutor-argument
- Put original TreeMap entries into newly created TreeMap for descending-order sorting of keys using putAll() method
SortTreeMapByKeysInDescendingOrder.java
package net.bench.resources.treemap.keys.sorting;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class SortTreeMapByKeysInDescendingOrder {
public static void main(String[] args) {
// 1. create TreeMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new TreeMap<>();
// 1.1 adding key-value pairs to TreeMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("Chinese", 409900000);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Mexico", 127792286);
countryPopulation.put("Pakistan", 220892331);
// 1.2 TreeMap - ascending-order
System.out.println("Original TreeMap in Ascending-order of Keys :- \n");
// 1.3 print Map entries using forEach() method
for(Map.Entry<String, Integer> entry : countryPopulation.entrySet()) {
System.out.println("Key : " + entry.getKey() + "\t\t"
+ "Value : " + entry.getValue());
}
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted TreeMap in "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 create new TreeMap passing comparator
Map<String, Integer> sortedMapDesc = new TreeMap<>(
Collections.reverseOrder());
// 3.2 add unsorted map to TreeMap for reverse-order sorting
sortedMapDesc.putAll(countryPopulation);
// 3.3 print Map entries using forEach() method
for(Map.Entry<String, Integer> entry : sortedMapDesc.entrySet()) {
System.out.println("Key : " + entry.getKey() + "\t\t"
+ "Value : " + entry.getValue());
}
}
}
Output:
Original TreeMap in Ascending-order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Chinese Value : 409900000
Key : Indian Value : 382357386
Key : Mexico Value : 127792286
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted TreeMap in reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Mexico Value : 127792286
Key : Indian Value : 382357386
Key : Chinese Value : 409900000
Key : Brazil Value : 213728559
Key : America Value : 332429717
Important points to remember about Map :
- HashMap stores entries (Key-Value pairs) in random-order of Keys
- LinkedHashMap stores entries (Key-Value pairs) as per insertion-order of Keys
- TreeMap stores entries (Key-Value pairs) in sorted-order of Keys
References:
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html