Date:

Share:

Sort Map by Key using TreeMap in Java

Related Articles

In this tutorial, we will learn how to use TreeMap to sort a map by its keys in Java

Sort map by simple key

Let’s start a Mapwhere is the map key It is a simple key of its kind 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 transferring an existing map in the constructor for example new TreeMap<>(map)It is sorted by the natural order of its keys, which is 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 reboot a TreeMap by passing a Comparator in the constructor new TreeMap<>(Comparator.reverseOrder()). In this case, all map values ​​are added to this TreeMap Sorted according to the previous equation.

Since we provided the Comparator to sort in reverse natural order, the resulting map is sorted in 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 start a Mapwhere is the map key It is a complex key of its kind ComplexKey class, which has several 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 for example new TreeMap<>(Comparator.comparing(ComplexKey::getName))Then all the map values ​​were added to it TreeMap Sorted according to the previous equation.

We passed the Comparator to sort the map by key object 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 several key characteristics

We can also pass a Comparator that sorts the map by several Key Object 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 start a TreeMap By passing a Comparator, which sorts the map by key object 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 several key characteristics

We can also pass a Comparator that sorts the map by several Key Object properties. For example, sort the map by priority property b Costs order and then by name property b going down 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}

Source

Popular Articles