A full Java example to demonstrate how to sort a Map (HashMap) based on its values. The overall idea is, convert the Map into List, sort the List and put the sorted list back to a Map.
Map ---> List ---> Sort ---> Map
Map sorting example
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 SortMapExample{ public static void main(String[] args) { System.out.println("Unsort Map......"); Map<String,String> unsortMap = new HashMap<String,String>(); unsortMap.put("1", "1"); unsortMap.put("2", "A"); unsortMap.put("3", "2"); unsortMap.put("4", "B"); unsortMap.put("5", "C"); unsortMap.put("6", "c"); unsortMap.put("7", "b"); unsortMap.put("8", "a"); Iterator iterator=unsortMap.entrySet().iterator(); for (Map.Entry entry : unsortMap.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println("Sorted Map......"); Map<String,String> sortedMap = sortByComparator(unsortMap); for (Map.Entry entry : sortedMap.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } private static Map sortByComparator(Map unsortMap) { List list = new LinkedList(unsortMap.entrySet()); //sort list based on comparator Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue()); } }); //put sorted list into map again Map sortedMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry)it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } }
Result
Unsort Map...... Key : 3 Value : 2 Key : 2 Value : A Key : 1 Value : 1 Key : 7 Value : b Key : 6 Value : c Key : 5 Value : C Key : 4 Value : B Key : 8 Value : a Sorted Map...... Key : 1 Value : 1 Key : 3 Value : 2 Key : 2 Value : A Key : 4 Value : B Key : 5 Value : C Key : 8 Value : a Key : 7 Value : b Key : 6 Value : c
No comments:
Post a Comment