How to sort a HashMap by values
As we saw in the articleHow to sort an ArrayList< T> . In this tutorial we will learn how to sort a HashMap in java in ascending and descending order. This is almost the same as in an ArrayList. We'll create a class named "Comparator" that implements the interface. Comparator to make the comparison in the two examples in this tutorial.1) Sort with Comparator and put the result in TreeMap
Why pass the instance of the Comparator class as a parameter in the TreeMap constructor? Simply because TreeMap< K, V> implements the Map interface as a HashMap and has a TreeMap(Comparator comp) constructor. The only difference is that TreeMap is sorted by keys.import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Tri {
public static void main(String[] args) {
HashMapmap = new HashMap ();
Comparator comp = new Comparator(map);
TreeMapmap_apres = new TreeMap (comp);
map.put("A",49);
map.put("B",18);
map.put("C",92);
map.put("D",37);
map.put("E",62);
System.out.println("Before sorting: "+map);
map_apres.putAll(map);
System.out.println("After sorting: "+map_apres);
}
}
class Comparator implements Comparator{
Maptuple;
public Comparator(HashMapmap) {
this.tuple = map;
}
//this comparator orders the elements in descending order
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
if (int) tuple.get(o1) >= (int) tuple.get(o2)) {
return -1;
} else {
return 1;
}
}
}
2) Sort HashMap with Collections.sort
In this example, we'll sort items of type Double. The items are copied into a LinkedList that implements the List interface, and then they are sorted with the Collections.sort that we used in previous articles to sort through other object collections. After sorting, we copy the sorted elements from LinkedList to a new hash table that will be returned as output.public static void main(String[] args) {Run:
HashMapmap = new HashMap ();
map.put("A",18.5);
map.put("E",76.8);
map.put("C",24.1);
map.put("F",86.2);
map.put("D",5.7);
map.put("B",84.6);
System.out.println("Before sorting: "+map);
System.out.println("After sorting: "+sortWithValue(map));
}
public static HashMap< String, Double> sortWithValue( HashMap< String, Double> map ){
List< Map.Entry< String, Double> > list =
new LinkedList< Map.Entry< String, Double> > ( map.entrySet() );
Collections.sort( list, new Comparator< Map.Entry< String, Double> > (){
public int compare( Map.Entry< String, Double> o1, Map.Entry< String, Double> o2 ){
return (o1.getValue()).compareTo( o2.getValue());
}
});
HashMap< String, Double> map_apres = new LinkedHashMap< String, Double> ();
for(Map.Entry< String, Double> entry : list)
map_apres.put( entry.getKey(), entry.getValue() );
return map_apres;
}
Before sorting: {D=5.7, E=76.8, F=86.2, A=18.5, B=84.6, C=24.1}To sort in descending direction, simply change the order of the comparison in the compare:
After sorting: {D=5.7, A=18.5, C=24.1, E=76.8, B=84.6, F=86.2}
References:return(o2.getValue()).compareTo( o1.getValue());
Oracle Documentation - HashMap
TutorialsPoint - The HashMap class
Documentation Comparator
LinkedHashMap Documentation