Sort Map by Key using TreeMap in Java
In this tutorial, we’ll learn how to use TreeMap to sort a Map by its keys in Java
Sort Map by Simple Key
Let’s initialize a Map
, where the map key is a simple key of type String
// initialize map in random order of keys
Map<String, String> map = Map.of(
"key5", "value5",
"key2", "value2",
"key4", "value4",
"key1", "value1",
"key3", "value3");
Sort Map in ascending order
When you initialize a TreeMap
by passing an existing map
in the constructor e.g. new TreeMap<>(map)
, it is sorted according to the natural ordering of its keys, which is the ascending alphabetical order for String
key type.
Map<String, String> sortedTreeMap = new TreeMap<>(map);
System.out.println(sortedTreeMap);
// {key1=value1, key2=value2, key3=value3, key4=value4, key5=value5}
Sort Map in descending order
You can also initialize a TreeMap
by passing a Comparator in the constructor new TreeMap<>(Comparator.reverseOrder())
. In this case, all the map entries added to this TreeMap
are sorted by the passed Comparator.
Since we have provided the Comparator to sort by reverse natural order, the resulting map is sorted by descending alphabetical order for String
key type.
Map<String, String> reverseSortedTreeMap = new TreeMap<>(Comparator.reverseOrder());
reverseSortedTreeMap.putAll(map);
System.out.print(reverseSortedTreeMap);
// {key5=value5, key4=value4, key3=value3, key2=value2, key1=value1}
Sort Map by Object Key
Let’s initialize a Map
, where the map key is a complex key of type ComplexKey
class, which has multiple properties - name
and priority
.
public class ComplexKey {
String name;
int priority;
}
// initialize map in random order of keys
Map<ComplexKey, String> map1 = Map.of(
new ComplexKey("key5", 1), "value5",
new ComplexKey("key2", 1), "value2",
new ComplexKey("key4", 1), "value4",
new ComplexKey("key1", 2), "value1",
new ComplexKey("key3", 2), "value3");
Sort Map in ascending order
When you initialize a TreeMap
by passing a Comparator in the constructor e.g. new TreeMap<>(Comparator.comparing(ComplexKey::getName))
, then all the map entries added to this TreeMap
are sorted by the passed Comparator.
We passed the Comparator to sort the Map by Key Object’s name
property in ascending order. Let’s see the result:-
Map<ComplexKey, String> sortedTreeMap = new TreeMap<>(Comparator.comparing(ComplexKey::getName));
sortedTreeMap.putAll(map);
System.out.println(sortedTreeMap);
// {ComplexKey(name=key1, priority=2)=value1,
// ComplexKey(name=key2, priority=1)=value2,
// ComplexKey(name=key3, priority=2)=value3,
// ComplexKey(name=key4, priority=1)=value4,
// ComplexKey(name=key5, priority=1)=value5}
Sort by multiple key properties
We can also pass a Comparator that sorts the map by multiple Key Object’s properties. For example, sort the map by priority
property and then by name
property in ascending order.
Map<ComplexKey, String> sortedTreeMap = new TreeMap<>(Comparator.comparingInt(ComplexKey::getPriority).thenComparing(ComplexKey::getName));
sortedTreeMap.putAll(map);
System.out.println(sortedTreeMap);
// {ComplexKey(name=key2, priority=1)=value2,
// ComplexKey(name=key4, priority=1)=value4,
// ComplexKey(name=key5, priority=1)=value5,
// ComplexKey(name=key1, priority=2)=value1,
// ComplexKey(name=key3, priority=2)=value3}
Sort Map in descending order
Let’s initialize a TreeMap
by passing a Comparator, which sorts the map by Key Object’s name
property in descending order.
Map<ComplexKey, String> reverseSortedTreeMap = new TreeMap<>(Comparator.comparing(ComplexKey::getName).reversed());
reverseSortedTreeMap.putAll(map);
System.out.println(reverseSortedTreeMap);
// {ComplexKey(name=key5, priority=1)=value5,
// ComplexKey(name=key4, priority=1)=value4,
// ComplexKey(name=key3, priority=2)=value3,
// ComplexKey(name=key2, priority=1)=value2,
// ComplexKey(name=key1, priority=2)=value1}
Sort by multiple key properties
We can also pass a Comparator that sorts the map by multiple Key Object’s properties. For example, sort the map by priority
property in ascending order and then by name
property in descending order.
Map<ComplexKey, String> reverseSortedTreeMapWithMultipleFields = new TreeMap<>(Comparator.comparingInt(ComplexKey::getPriority)
.thenComparing((c1, c2) -> c2.getName().compareTo(c1.getName())));
reverseSortedTreeMapWithMultipleFields.putAll(map);
System.out.println(reverseSortedTreeMapWithMultipleFields);
// {ComplexKey(name=key5, priority=1)=value5,
// ComplexKey(name=key4, priority=1)=value4,
// ComplexKey(name=key2, priority=1)=value2,
// ComplexKey(name=key3, priority=2)=value3,
// ComplexKey(name=key1, priority=2)=value1}