In the last tutorial we have shared how to remove all mappings from the HashMap in java. In this tutorial we will learn how to sort HashMap by keys using TreeMap and by values using Comparator.
I have already shared how TreeMap internally works in java.
HashMap Sorting by Keys
In this example we are sorting the HashMap based on the keys using TreeMap collection class.
Program for Sorting HashMap by Keys
Related Articles
import java.util.*;
public class HashMapSortByKeyExample {
public static void main(String args[]) {
// Creating a HashMap of int keys and String values
HashMapInteger, String> hashmap = new HashMapInteger, String>();
// Adding Key and Value pairs to HashMap
hashmap.put(22,"A");
hashmap.put(55,"B");
hashmap.put(33,"Z");
hashmap.put(44,"M");
hashmap.put(99,"I");
hashmap.put(88,"X");
System.out.println("Before Sorting:");
Set set = hashmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
System.out.print(pair.getKey() + ": ");
System.out.println(pair.getValue());
}
MapInteger, String> map = new TreeMapInteger, String>(hashmap); System.out.println("After Sorting:");
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry pair = (Map.Entry)iterator2.next();
System.out.print(pair.getKey() + ": ");
System.out.println(pair.getValue());
}
}
}
Output
Before Sorting:
33: Z
99: I
22: A
55: B
88: X
44: M
After Sorting:
22: A
33: Z
44: M
55: B
88: X
99: I
HashMap Sorting by Values
In this example we are sorting HashMap by values using Comparator.
Program for Sorting HashMap by Values
import java.util.*;
public class HashMapSortByValueExample {
public static void main(String args[]) {
// Creating a HashMap of int keys and String values
HashMapInteger, String> hashmap = new HashMapInteger, String>();
// Adding Key and Value pairs to HashMap
hashmap.put(22,"A");
hashmap.put(55,"B");
hashmap.put(33,"Z");
hashmap.put(44,"M");
hashmap.put(99,"I");
hashmap.put(88,"X");
System.out.println("Before Sorting:");
Set set = hashmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
System.out.print(pair.getKey() + ": ");
System.out.println(pair.getValue());
}
MapInteger, String> map = sortByValues(hashmap);
System.out.println("After Sorting:");
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry pair = (Map.Entry)iterator2.next();
System.out.print(pair.getKey() + ": ");
System.out.println(pair.getValue());
}
}
private static HashMap sortByValues(HashMap map) {
List list = new LinkedList(map.entrySet());
// Defined Custom Comparator here
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
// Here I am copying the sorted list in HashMap
// using LinkedHashMap to preserve the insertion order
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
return sortedHashMap;
}
}
Output
Before Sorting:
33: Z
99: I
22: A
55: B
88: X
44: M
After Sorting:
22: A
55: B
99: I
44: M
88: X
33: Z
References : HashMap, LinkedHashMap, Comparator, TreeMap