How to sort Map on the basis of values

Nishant Malhotra
2 min readAug 22, 2021

--

Map is most common collection used in java usually to store key-value pairs. Map interface in java has its own implementation classes which can be seen in below hierarchy

Now for any collection filtering or sorting are the most common operations that are mostly required. Today in this article I tried to explain how to sort a map based on the values it holds.

In java 8, Map.Entry class has static method comparingByValue() to help you in sorting by values. This method returns a Comparator that compares Map.Entry in natural order on values. Alternatively, you can pass a custom Comparator to use in sorting. This can be used to sort the map in reverse order.

You can also find the formatted code on below URL:

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import static java.util.stream.Collectors.*;
import static java.util.Map.Entry.*;
/**
* @author Nishant
*
*/
public class SortingHashMapByValue {
public static void main(String[] args) throws Exception {SortingHashMapByValue obj = new SortingHashMapByValue();
System.out.println("Map before sorting: " + obj.getMapToSort());
/** After sorting the map on values. **/
System.out.println("Map after sorting by value: " + obj.sortMapByValue(obj.getMapToSort()));
/** After sorting the map on values in reverse order. **/
System.out.println(
"Map after sorting by value in reverse order: " + obj.sortMapByValueInReverse(obj.getMapToSort()));
}/**
* This is method to sort the objects by value.
*
* @param categoryProductMap
* @return map of sorted objects
*/
private Map<Object, Object> sortMapByValue(Map<String, String> categoryProductMap) {
return categoryProductMap.entrySet().stream().sorted(comparingByValue())
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
}
/**
* This is method to sort the objects by value in reverse order.
*
* @param categoryProductMap
* @return map of sorted objects
*/
private Map<Object, Object> sortMapByValueInReverse(Map<String, String> categoryProductMap) {
return categoryProductMap.entrySet().stream().sorted(Collections.reverseOrder(comparingByValue()))
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
}
/**
* Method to get Map to be sorted.
*
* @return categoryProductMap categoryProductMap
*/
private Map<String, String> getMapToSort() {
// a Map with string keys and integer values
Map<String, String> categoryProductMap = new HashMap<String, String>();
categoryProductMap.put("clothes", "Shirt");
categoryProductMap.put("grocery", "Wheat");
categoryProductMap.put("transport", "Scooter");
categoryProductMap.put("utility", "Rack");
categoryProductMap.put("gift", "Sweet");
categoryProductMap.put("miscellaneous", "Member");
return categoryProductMap;
}
}

In above code , category product Map is sorted based on the values in both alphabetical order and reverse alphabetical order as well . Here is the output of above program :

Map before sorting: {gift=Sweet, grocery=Wheat, utility=Rack, transport=Scooter, miscellaneous=Member, clothes=Shirt}Map after sorting by value: {miscellaneous=Member, utility=Rack, transport=Scooter, clothes=Shirt, gift=Sweet, grocery=Wheat}Map after sorting by value in reverse order: {grocery=Wheat, gift=Sweet, clothes=Shirt, transport=Scooter, utility=Rack, miscellaneous=Member}

Hope this article helps . Happy learning!!

--

--

Nishant Malhotra

Passionate Developer and Architect in Java and related frameworks with 13 years of varied experience across domains and practices.