Sort a Map in Java Example
A map is an data structure class that stores associations between keys and values which are widely used in java programming language. As map stores key-value pair, we can can sort map by two ways.
- Sort by keys
- Sort by values
Let us see example to sort map in both ways.
1) Sort a Map by keys
There are several ways to sort a map in java. I would like to use TreeMap class to sort a unsorted map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.code2succeed.sorting.map; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class OrderByKey { public static void main(String[] args) { Map<Integer, String> unsortedMap = new HashMap<Integer, String>(); unsortedMap.put(1, "a"); unsortedMap.put(20, "c"); unsortedMap.put(11, "e"); unsortedMap.put(15, "b"); unsortedMap.put(7, "d"); System.out.println("Unsorted Map : "+unsortedMap); Map<Integer, String> sortedMap = new TreeMap<Integer, String>(unsortedMap); System.out.println("Sorted Map : "+sortedMap); } } |
Output
1 2 |
Unsorted Map : {1=a, 20=c, 7=d, 11=e, 15=b} Sorted Map : {1=a, 7=d, 11=e, 15=b, 20=c} |
In above example we have sorted a map which contains Integer as key and String as value. What if I have vice versa, String as key and Integer as value? The answer of this question is same use TreeMap class to sort the unsorted map.
Here is the example to sort unsorted map with String as key and Integer as value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.code2succeed.sorting.map; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class OrderByKey { public static void main(String[] args) { Map<String, Integer> unsortedMap = new HashMap<String, Integer>(); unsortedMap.put("a", 1); unsortedMap.put("c", 20); unsortedMap.put("e", 11); unsortedMap.put("b", 15); unsortedMap.put("d", 7); System.out.println("Unsorted Map : "+unsortedMap); Map<String, Integer> sortedMap = new TreeMap<String, Integer>(unsortedMap); System.out.println("Sorted Map : "+sortedMap); } } |
Output
1 2 |
Unsorted Map : {d=7, e=11, b=15, c=20, a=1} Sorted Map : {a=1, b=15, c=20, d=7, e=11} |
2) Sort a Map by values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.code2succeed.sorting.map; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class OderByValue { public static void main(String[] args) { Map<Integer, String> unsortedMap = new HashMap<Integer, String>(); unsortedMap.put(1, "a"); unsortedMap.put(20, "c"); unsortedMap.put(11, "e"); unsortedMap.put(15, "b"); unsortedMap.put(7, "d"); System.out.println("Unsorted Map: "+unsortedMap); // Convert Map to List List<Map.Entry<Integer, String>> list = new ArrayList<Map.Entry<Integer, String>>(unsortedMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() { public int compare(Map.Entry<Integer, String> o1, Map.Entry<Integer, String> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // Convert sorted map back to a Map Map<Integer, String> sortedMap = new LinkedHashMap<Integer, String>(); for (Iterator<Map.Entry<Integer, String>> it = list.iterator(); it.hasNext();) { Map.Entry<Integer, String> entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } System.out.println("Sorted Map by Value: "+sortedMap); } } |
Output
1 2 |
Unsorted Map: {1=a, 20=c, 7=d, 11=e, 15=b} Sorted Map by Value: {1=a, 15=b, 20=c, 7=d, 11=e} |