The below examples use a Map where country name is key and the name of its capital is the corresponding value.
Here, we will discuss both :
– Sort Map by key
– Sort Map by value
If you are using Java8, refer this article instead:
Sort a Map in Java 8
Sort a Map by key
Example 1: Using TreeMap
This example sorts the countryCapitalMap in ascending order of keys(country names) using a TreeMap.
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 | package com.topjavatutorial; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class MapSortByKeyExample { public static void main(String[] args) { Map<String, String> countryCapitalMap = new HashMap<String, String>(); countryCapitalMap.put("guyana", "georgetown"); countryCapitalMap.put("nepal", "kathmandu"); countryCapitalMap.put("australia", "canberra"); countryCapitalMap.put("india", "new delhi"); countryCapitalMap.put("japan", "tokyo"); System.out.println("Original Map : \n" + countryCapitalMap); Map<String, String> sortedMap = new TreeMap<String, String>(countryCapitalMap); System.out.println("Map sorted by key : \n" + sortedMap); } } |
Output:
Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}
Map sorted by key :
{australia=canberra, guyana=georgetown, india=new delhi, japan=tokyo, nepal=kathmandu}
However, this solution may not work if you have multiple keys mapping to the same value. The next approach is more preferable.
Example 2 : Using Comparator
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 44 45 | package com.topjavatutorial; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class MapSortByKeyExample { public static void main(String[] args) { Map<String, String> countryCapitalMap = new HashMap<String, String>(); countryCapitalMap.put("guyana", "georgetown"); countryCapitalMap.put("nepal", "kathmandu"); countryCapitalMap.put("australia", "canberra"); countryCapitalMap.put("india", "new delhi"); countryCapitalMap.put("japan", "tokyo"); System.out.println("Original Map : \n" + countryCapitalMap); List<Map.Entry<String, String>> list = new LinkedList<Map.Entry<String, String>>( countryCapitalMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return (o1.getKey()).compareTo(o2.getKey()); } }); Map<String, String> sortedMap = new LinkedHashMap<String, String>(); for (Map.Entry<String, String> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } System.out.println("Map sorted by key : \n" + sortedMap); } } |
Output:
Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}
Map sorted by key :
{australia=canberra, guyana=georgetown, india=new delhi, japan=tokyo, nepal=kathmandu}
If you want the map sorted in reverse(descending) order of keys, just change the compare() in Comparator accordingly :
1 2 3 4 5 6 7 | public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return (o2.getKey()).compareTo(o1.getKey()); } |
Sort a Map by value
This example sorts the countryCapitalMap in ascending order of values(country capitals).
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 44 45 | package com.topjavatutorial; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class MapSortByValueExample { public static void main(String[] args) { Map<String, String> countryCapitalMap = new HashMap<String, String>(); countryCapitalMap.put("guyana", "georgetown"); countryCapitalMap.put("nepal", "kathmandu"); countryCapitalMap.put("australia", "canberra"); countryCapitalMap.put("india", "new delhi"); countryCapitalMap.put("japan", "tokyo"); System.out.println("Original Map : \n" + countryCapitalMap); List<Map.Entry<String, String>> list = new LinkedList<Map.Entry<String, String>>( countryCapitalMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); Map<String, String> sortedMap = new LinkedHashMap<String, String>(); for (Map.Entry<String, String> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } System.out.println("Map sorted by value : \n" + sortedMap); } } |
Output:
Original Map :
{guyana=georgetown, japan=tokyo, nepal=kathmandu, australia=canberra, india=new delhi}
Map sorted by value :
{australia=canberra, guyana=georgetown, nepal=kathmandu, india=new delhi, japan=tokyo}
If you are using Java8, refer this article instead:
© 2016 – 2017, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post