In this article, we will discuss how to convert unordered HashMap into Sorted TreeMap
We can sort HashMap in both ascending and descending order, by passing HashMap contents as argument to TreeMap’s inter-conversion constructor
Solution:
- Ascending order: pass HashMap entries as arguments to TreeMap class’ inter-conversion constructor
- Descending order: Implement Comparator interface by providing reverse sorting logic and finally putting all entries of HashMap into TreeMap classs using putAll() method
Example 1: Sorting in ascending order of keys:
- Converting Unordered HashMap to Sorted TreeMap in ascending order
Syntax:
TreeMap<String, String> set = new TreeMap<String, String>(hashMap);
SortingHashMapInAscendingOrder.java
package in.bench.resources.java.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class SortingHashMapInAscendingOrder { public static void main(String[] args) { // creating HashMap object of type <String, String> HashMap<String, String> hm = new HashMap<String, String>(); // adding key-value pairs to HashMap object hm.put("Microsoft", "Bill Gates"); hm.put("Apple", "Steve Jobs"); hm.put("Google", "Sundar Pichai"); hm.put("LinkedIn", "Reid Hoffman"); hm.put("Facebook", "Mark Zuckerberg"); // Random Order iterating System.out.println("Before Sorting : Random Order \n"); // getting keySet() into Set Set<String> set = hm.keySet(); // get Iterator from key set Iterator<String> itr = set.iterator(); // iterating in random order while(itr.hasNext()) { String key = itr.next(); System.out.println("Key : " + key + "\t\t" + "Value : " + hm.get(key)); } // After Sorting : Ascending order System.out.println("\n\n\nAfter Sorting : Ascending order\n"); // convert to TreeMap Map<String, String> ts = new TreeMap<String, String>(hm); // iterate acc to ascending order of keys for(String strKey : ts.keySet()){ System.out.println("Key : " + strKey + "\t\t" + "Value : " + hm.get(strKey)); } } }
Output:
Before Sorting : Random Order Key : Apple Value : Steve Jobs Key : Microsoft Value : Bill Gates Key : Facebook Value : Mark Zuckerberg Key : LinkedIn Value : Reid Hoffman Key : Google Value : Sundar Pichai After Sorting : Ascending order Key : Apple Value : Steve Jobs Key : Facebook Value : Mark Zuckerberg Key : Google Value : Sundar Pichai Key : LinkedIn Value : Reid Hoffman Key : Microsoft Value : Bill Gates
Example 2: Sorting in descending order of keys:
- Converting Unordered HashMap to Sorted TreeMap in descending order
Syntax:
TreeMap<String, String> map = new TreeMap<String, String>(reverseCompLogic); map.putAll(hashMap);
SortingHashMapInDescendingOrder.java
package in.bench.resources.java.map; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class SortingHashMapInDescendingOrder { public static void main(String[] args) { // creating HashMap object of type <String, String> HashMap<String, String> hm = new HashMap<String, String>(); // adding key-value pairs to HashMap object hm.put("Microsoft", "Bill Gates"); hm.put("Apple", "Steve Jobs"); hm.put("Google", "Sundar Pichai"); hm.put("LinkedIn", "Reid Hoffman"); hm.put("Facebook", "Mark Zuckerberg"); // Random Order iterating System.out.println("Before Sorting : Random Order \n"); // getting keySet() into Set Set<String> set = hm.keySet(); // get Iterator from key set Iterator<String> itr = set.iterator(); // iterating in random order while(itr.hasNext()) { String key = itr.next(); System.out.println("Key : " + key + "\t\t" + "Value : " + hm.get(key)); } // After Sorting : Ascending order System.out.println("\n\n\nAfter Sorting : Descending order\n"); // convert to TreeMap Map<String, String> ts = new TreeMap<String, String>( Collections.reverseOrder()); // put all key-value into TreeMap ts.putAll(hm); // iterate acc to descending order of keys for(String strKey : ts.keySet()){ System.out.println("Key : " + strKey + "\t\t" + "Value : " + hm.get(strKey)); } } }
Output:
Before Sorting : Random Order Key : Google Value : Sundar Pichai Key : Microsoft Value : Bill Gates Key : Facebook Value : Mark Zuckerberg Key : LinkedIn Value : Reid Hoffman Key : Apple Value : Steve Jobs After Sorting : Descending order Key : Microsoft Value : Bill Gates Key : LinkedIn Value : Reid Hoffman Key : Google Value : Sundar Pichai Key : Facebook Value : Mark Zuckerberg Key : Apple Value : Steve Jobs
String and Wrapper classes v/s Custom Object:
- Since, we are dealing with String objects
- actually we don’t have to do much work here
- as String class’ already implements Comparable interface
- otherwise, we have to code for object comparison as explained in below articles,
- Custom object with Comparator interface
- Custom object with Comparable interface
Important points:
- HashMap stores entries (key-value pairs) in random-order
- LinkedHashMap stores entries (key-value pairs) as per insertion-order
- TreeMap stores entries (key-value pairs) in some sorting-order
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/class-use/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!