Today, you will learn about the Java 8 sort map by value descending. We will cover different examples to sort map by keys, values and sorting in reverse order using Java 8 Stream API. Before we start, let’s discovery some methods which we are using in this tutorial, below:
Stream.sorted() – Sort elements base on Comparator.
comparingByKey() – Returns a comparator that compares Map.Entry in natural order on key.
Sort key in reversed order
comparingByKey() – Returns a comparator that compares Map.Entry in natural order on key.
comparingByKey().reversed() – Returns a comparator that imposes the reverse ordering of this comparator.
Sort by value
comparingByValue() – Returns a comparator that compares Map.Entry in natural order on value.
Apply limit
comparingByKey() – Returns a comparator that compares Map.Entry in natural order on key.
comparingByKey().reversed() – Returns a comparator that imposes the reverse ordering of this comparator.
forEachOrdered() – Print the result with order.
Sort Map by Keys
Here, we’re using the method Map.Entry.comparingByKey() to sort a map by keys and return a new LinkedHashMap, we need LinkedHashMap to keep the order. toMap() method will returns HashMap by default so we need to pass the reference method LinkedHashMap::new to force it returns a LinkedHashMap.
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 |
public class SortMapExample { public static void main(String[] argv) { Map<String, Integer> map = new HashMap<>(); map.put("k1", 101); map.put("k2", 50); map.put("k10", 13); map.put("k4", 20); map.put("k3", 109); map.put("k8", 78); map.put("k22", 81); System.out.println("The original map:"); System.out.println(map); Map<String, Integer> result = map.entrySet().stream().sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); System.out.println("The sortedMap:"); System.out.println(result); } } |
Output
1 2 3 4 |
The original map: {k1=101, k2=50, k3=109, k4=20, k22=81, k10=13, k8=78} The sortedMap: {k1=101, k10=13, k2=50, k22=81, k3=109, k4=20, k8=78} |
Sort Map by Values
Here, we’re using the method Map.Entry.comparingByValue() to sort a map by values
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 |
public class SortMapExample2 { public static void main(String[] argv) { Map<String, Integer> map = new HashMap<>(); map.put("k1", 101); map.put("k2", 50); map.put("k10", 13); map.put("k4", 20); map.put("k3", 109); map.put("k8", 78); map.put("k22", 81); System.out.println("The original map:"); System.out.println(map); // sort by values Map<String, Integer> result = map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors .toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); // sort by values, and reserve it Map<String, Integer> result2 = map.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); System.out.println("The Sorted map:"); System.out.println(result); System.out.println(result2); } } |
Output
That’s all about the Java 8 sort map by value descending.
References
Map.Entry (Java Platform SE 8 )
Download the complete source code, click link below
SortMapExample.zip (241 downloads)