Sorted Map Example – By Value or By Key or By Comparator
In this Sorted Map Example we will show how you can sort a java.util.Map
using the java.util.TreeMap
Class. We can sort collections in Ascending or Descending order by key or value. We also show you how you can sort a collection using Java 8 Lambda expression.
Sorted Map By Key Example
You can sort a java.util.map
by putting all the entries in a java.util.TreeMap
.
package com.memorynotfound.collections.map;
import java.util.*;
public class SortByKey {
public static void main(String... args){
Map<Integer, String> random = new HashMap<Integer, String>();
for (int i = 0; i < 10; i++) {
random.put((int)(Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
}
System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));
TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(random);
System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
}
}
Initial Map: [0=10, 98=74, 3=22, 99=66, 84=10, 37=18, 38=86, 76=34, 45=7]
Sorted Map: [0=10, 3=22, 37=18, 38=86, 45=7, 76=34, 84=10, 98=74, 99=66]
Sorted Map By Key Reverse using comparator Example
If you want to sort a java.util.Map
in descending order you can use a custom Comparator.
package com.memorynotfound.collections.map;
import java.util.*;
public class SortByKeyReverse {
public static void main(String... args){
Map<Integer, String> random = new HashMap<Integer, String>();
for (int i = 0; i < 10; i++) {
random.put((int)(Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
}
System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));
TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
sorted.putAll(random);
System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
}
}
Initial Map: [64=53, 1=59, 65=17, 97=82, 34=90, 4=93, 86=48, 89=32, 95=22, 63=93]
Reversed Sorted Map: [97=82, 95=22, 89=32, 86=48, 65=17, 64=53, 63=93, 34=90, 4=93, 1=59]
Sorted Map By Value using custom comparator Example
If you want to sort a java.util.Map
by value, you can create a custom Comparator.
package com.memorynotfound.collections.map;
import java.util.Comparator;
import java.util.Map;
public class ValueComparator implements Comparator<Integer> {
private Map<Integer, String> map;
public ValueComparator(Map<Integer, String> map) {
this.map = map;
}
public int compare(Integer a, Integer b) {
return map.get(a).compareTo(map.get(b));
}
}
Next we can use the custom value comparator to sort a Map by Value.
package com.memorynotfound.collections.map;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortByValue {
public static void main(String... args){
Map<Integer, String> random = new HashMap<Integer, String>();
for (int i = 0; i < 10; i++) {
random.put((int)(Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
}
System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));
TreeMap<Integer, String> sorted = new TreeMap<Integer, String>(new ValueComparator(random));
sorted.putAll(random);
System.out.println("Sorted Map: " + Arrays.toString(sorted.entrySet().toArray()));
}
}
Initial Map: [35=59, 19=24, 52=44, 53=25, 25=7, 58=95, 74=38, 93=47, 62=37]
Sorted Map: [19=24, 53=25, 62=37, 74=38, 52=44, 93=47, 35=59, 25=7, 58=95]
Java 8 Lambda Sorted Map By Value Example
Java 8 has a new streaming API on top of the collections framework where you can effectively sort a java.util.Map
by value.
package com.memorynotfound.collections.map;
import java.util.*;
import java.util.stream.Collectors;
public class SortByValueLambda {
public static void main(String... args){
Map<Integer, String> random = new HashMap<Integer, String>();
for (int i = 0; i < 10; i++) {
random.put((int)(Math.random() * 100), String.valueOf((int) (Math.random() * 100)));
}
System.out.println("Initial Map: " + Arrays.toString(random.entrySet().toArray()));
Map<Integer, String> sortedMap =
random.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Sorted Map: " + Arrays.toString(sortedMap.entrySet().toArray()));
}
}
Initial Map: [1=8, 65=11, 33=2, 85=50, 70=69, 23=60, 71=25, 59=60, 13=47, 62=47]
Sorted Map: [65=11, 33=2, 71=25, 13=47, 62=47, 85=50, 23=60, 59=60, 70=69, 1=8]