Skip to content
Related Articles

Related Articles

Iterate TreeMap in Reverse Order in Java
  • Last Updated : 28 Dec, 2020

The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys.

There are three simple ways to iterate TreeMap in reverse order in Java:

  1. Using the reverseOrder() method
  2. Using the descendingKeySet() method
  3. Using the descendingMap() method

Method 1:

The reverseOrder() method of the Collections class returns a Comparator that imposes the reverse of the natural ordering of the objects. Use this in the constructor of the TreeMap to create an object that stores the mapping in the reverse order of the keys.

// Use reverseOrder() method in the constructor
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(Collections.reverseOrder());



Below is the implementation:

Java




// Java Program to iterate TreeMap in Reverse Order in Java
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Use reverseOrder() method in the constructor
        TreeMap<Integer, String> treeMap
            = new TreeMap<>(Collections.reverseOrder());
  
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
  
        // Print the TreeMap in reverse order of the keys
        System.out.println("TreeMap in reverse order: "
                           + treeMap);
    }
}
Output
TreeMap in reverse order: {4=geeksforgeeks, 3=on, 2=geeks, 1=Hello}

Time Complexity: O(N)

 

Method 2:

The descendingKeySet() method returns a reverse order Set view of the keys. So iterate over the set view that returns the TreeMap keys in the descending order and get the value with the help of get() method.

Note: The key set returned by the descendingKeySet() method is a view and it is backed by the original TreeMap object. Any changes made to this view will be reflected in the original TreeMap object and vice versa.

Below is the implementation:



Java




// Java Program to iterate TreeMap in Reverse Order in Java
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New TreeMap
        TreeMap<Integer, String> treeMap = new TreeMap<>();
  
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
  
        // Print the TreeMap
        System.out.println("TreeMap before reverse: "
                           + treeMap);
  
        // view set of the keys in reverseOrder
        Set<Integer> keySet = treeMap.descendingKeySet();
  
        // After reverse
        System.out.println("TreeMap after reverse:");
  
        // Iterate view set of the keys
        // and get value of the key
        for (Integer key : keySet) {
            // Print key:value of the TreeMap
            System.out.println(key + " = "
                               + treeMap.get(key));
        }
    }
}
Output
TreeMap before reverse: {1=Hello, 2=geeks, 3=on, 4=geeksforgeeks}
TreeMap after reverse:
4 = geeksforgeeks
3 = on
2 = geeks
1 = Hello

Time Complexity: O(N)

 

Method 3:

The descendingMap() method of the TreeMap class returns a map containing a reverse view of the mappings. Iterate over the map using the iterator of the entry set.

Note: The descending map returned by the descendingMap() method is a view so any changes made will be reflected in the original TreeMap and vice versa.

Below is the implementation:

Java




// Java Program to iterate TreeMap in Reverse Order in Java
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New TreeMap
        TreeMap<Integer, String> treeMap = new TreeMap<>();
  
        // Add elements to treeMap
        treeMap.put(1, "Hello");
        treeMap.put(2, "geeks");
        treeMap.put(3, "on");
        treeMap.put(4, "geeksforgeeks");
  
        // Before reverse
        System.out.println("TreeMap before reverse:"
                           + treeMap);
  
        // view map contaning reverse view of mapping
        Map<Integer, String> reverseMap
            = treeMap.descendingMap();
  
        // After reverse
        System.out.println("TreeMap after reverse:"
                           + reverseMap);
    }
}
Output
TreeMap before reverse:{1=Hello, 2=geeks, 3=on, 4=geeksforgeeks}
TreeMap after reverse:{4=geeksforgeeks, 3=on, 2=geeks, 1=Hello}

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :