1. Sort a Map by Keys
To sort a Map by its keys, uses TreeMap, it sort Map keys automatically.
1.1 Review a “Map keys” in “String” example.
SortMapOnKeyStringExample.java
package com.mkyong;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortMapOnKeyStringExample {
public static void main(String[] args) {
Map<String, String> unsortMap = new HashMap<String, String>();
unsortMap.put("Z", "z");
unsortMap.put("B", "b");
unsortMap.put("A", "a");
unsortMap.put("C", "c");
unsortMap.put("D", "d");
unsortMap.put("E", "e");
unsortMap.put("Y", "y");
unsortMap.put("N", "n");
unsortMap.put("J", "j");
unsortMap.put("M", "m");
unsortMap.put("F", "f");
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("\nSorted Map......");
Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);
printMap(treeMap);
}
public static void printMap(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
}
}
OUTPUT:
Unsort Map......
Key : D Value : d
Key : E Value : e
Key : F Value : f
Key : A Value : a
Key : B Value : b
Key : C Value : c
Key : M Value : m
Key : N Value : n
Key : Y Value : y
Key : J Value : j
Key : Z Value : z
OUT PUT:
Sorted Map......
Key : A Value : a
Key : B Value : b
Key : C Value : c
Key : D Value : d
Key : E Value : e
Key : F Value : f
Key : J Value : j
Key : M Value : m
Key : N Value : n
Key : Y Value : y
Key : Z Value : z
1.2 Review a “Map keys” in “Integer” example.
SortMapOnKeyIntegerExample.java
package com.mkyong;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortMapOnKeyIntegerExample {
public static void main(String[] args) {
Map<Integer, String> unsortMap = new HashMap<Integer, String>();
unsortMap.put(10, "z");
unsortMap.put(5, "b");
unsortMap.put(6, "a");
unsortMap.put(20, "c");
unsortMap.put(1, "d");
unsortMap.put(7, "e");
unsortMap.put(8, "y");
unsortMap.put(99, "n");
unsortMap.put(50, "j");
unsortMap.put(2, "m");
unsortMap.put(9, "f");
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("\nSorted Map......");
Map<Integer, String> treeMap = new TreeMap<Integer, String>(
new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
treeMap.putAll(unsortMap);
printMap(treeMap);
}
public static void printMap(Map<Integer, String> map) {
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
}
}
OUTPUT:
Unsort Map......
Key : 50 Value : j
Key : 1 Value : d
Key : 2 Value : m
Key : 99 Value : n
Key : 20 Value : c
Key : 5 Value : b
Key : 6 Value : a
Key : 7 Value : e
Key : 8 Value : y
Key : 9 Value : f
Key : 10 Value : z
Sorted Map......
Key : 99 Value : n
Key : 50 Value : j
Key : 20 Value : c
Key : 10 Value : z
Key : 9 Value : f
Key : 8 Value : y
Key : 7 Value : e
Key : 6 Value : a
Key : 5 Value : b
Key : 2 Value : m
Key : 1 Value : d
By default, it will sorts the “Integer” keys by ascending order, 0-10. In this example, we add a Comparator to TreeMap and make it sorts keys by descending order.
2. Sort a Map by Values
TreeMap is unable to sort the Map values, instead, we should use Comparator.
The overall idea is, converts the Map into a List, sorts the List by Comparator and put the sorted list back to a Map.
Map ---> List ---> Sort --> SortedList ---> Map
2.1 Review a Map values in “Integer” example.
SortMapOnValueIntegerExample.java
package com.hostingcompass.jobs;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class SortMapOnValueIntegerExample {
public static void main(String[] args) {
Map<String, Integer> unsortMap = new HashMap<String, Integer>();
unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("j", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);
System.out.println("Unsort Map......");
printMap(unsortMap);
System.out.println("\nSorted Map......");
Map<String, Integer> sortedMap = sortByComparator(unsortMap);
printMap(sortedMap);
}
private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) {
// Convert Map to List
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
// Sort list with comparator, to compare the Map values
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
// Convert sorted map back to a Map
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public static void printMap(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("[Key] : " + entry.getKey()
+ " [Value] : " + entry.getValue());
}
}
}
Output
Unsort Map......
Sorted Map......