Java HashMap sort by values example shows how to sort HashMap by values in Java using the sort method, the LinkedHashMap class. and a Comparator.
How to sort HashMap by values in Java?
The HashMap class by definition does not maintain the order of the inserted elements. So even if we sort it, it might not return the elements in the same order. From HashMap Javadoc,
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
If you want to sort HashMap by key, you can use the implementation of SortedMap like TreeMap, which will automatically sort the Map by keys. Sorting HashMap by values will need a collection that maintains order like LinkedHashMap.
a) Sorting HashMap by value if you want only values
If you want only values of the HashMap to be sorted in a particular order and don’t need the whole map, then it can be done with the help of the Set as given below.
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 |
package com.javacodeexamples.basic.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; public class SortHashMapByValuesExample { public static void main(String[] args) { //create a new HashMap HashMap<String, Integer> hMap = new HashMap<String, Integer>(); //put values hMap.put("Three", 3); hMap.put("Two", 2); hMap.put("Four", 4); hMap.put("One", 1); hMap.put("Five", 5); //get all the values from the HashMap List<Integer> list = new ArrayList<Integer>( hMap.values() ); /* * Use sort method of Collections to sort the List and * provide custom Comparator to compare values. */ Collections.sort(list, new Comparator<Integer>(){ public int compare(Integer i1, Integer i2) { return i1.compareTo(i2); } }); //print the sorted values List System.out.println("Sorted Values"); for(Integer value : list) System.out.println(value); } } |
Output
1 2 3 4 5 6 7 |
Output Sorted Values 1 2 3 4 5 |
In the above example, first, we took all the HashMap values into the List. We then used the sort
method of the Collections class to sort the list. We also provided a custom Comparator so that value objects (in our case Integer objects) can be compared to one another while being sorted.
How to sort HashMap by value in descending order?
If you want to sort HashMap by values in descending order change the line which sorts the list as given below.
1 2 3 4 5 6 7 |
Collections.sort(list, new Comparator<Integer>(){ public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } }); |
Output
1 2 3 4 5 6 |
Sorted Values 5 4 3 2 1 |
b) Sorting HashMap by values if you want both keys and values
In the above example, we sorted the HashMap values. If you want a Map containing keys and values which is sorted by values based on the original HashMap, you need to sort the HashMap and put keys and values in a collection that maintains the element order such as LinkedHashMap as given below.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.javacodeexamples.basic.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class SortHashMapByValuesExample { public static void main(String[] args) { //create a new HashMap HashMap<String, Integer> hMap = new HashMap<String, Integer>(); //put values hMap.put("Three", 3); hMap.put("Two", 2); hMap.put("Four", 4); hMap.put("One", 1); hMap.put("Five", 5); //get map sorted by values Map<String, Integer> sortedMapByValues = getSortedMapByValues(hMap); //print sorted map for(Map.Entry<String, Integer> entry : sortedMapByValues.entrySet()){ System.out.println( entry.getKey() + " => " + ": " + entry.getValue() ); } } private static <K extends Comparable, V extends Comparable> Map<K, V> getSortedMapByValues(final Map<K, V> map){ Map<K, V> mapSortedByValues = new LinkedHashMap<K, V>(); //get all the entries from the original map and put it in a List List<Map.Entry<K, V>> list = new ArrayList<Entry<K, V>>(map.entrySet()); //sort the entries based on the value by custom Comparator Collections.sort(list, new Comparator<Map.Entry<K, V>>(){ public int compare(Entry<K, V> entry1, Entry<K, V> entry2) { return entry1.getValue().compareTo( entry2.getValue() ); } }); //put all sorted entries in LinkedHashMap for( Map.Entry<K, V> entry : list ) mapSortedByValues.put(entry.getKey(), entry.getValue()); //return Map sorted by values return mapSortedByValues; } } |
Output
1 2 3 4 5 6 |
HashMap sorted by values One => : 1 Two => : 2 Three => : 3 Four => : 4 Five => : 5 |
Note:
If the HashMap contains an object of a custom class as value, the custom class must implement the Comparable interface for the above method to work.
This example is a part of the Java HashMap tutorial, and Java LinkedHashMap tutorial.
Please let me know your views in the comments section below.