Monday, November 18, 2024
Google search engine
HomeLanguagesJavaIterate TreeMap in Reverse Order in Java

Iterate TreeMap in Reverse Order in Java

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, "neveropen");
 
        // Print the TreeMap in reverse order of the keys
        System.out.println("TreeMap in reverse order: "
                           + treeMap);
    }
}


 
 

Output

TreeMap in reverse order: {4=neveropen, 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, "neveropen");
 
        // 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=neveropen}
TreeMap after reverse:
4 = neveropen
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, "neveropen");
 
        // Before reverse
        System.out.println("TreeMap before reverse:"
                           + treeMap);
 
        // view map containing 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=neveropen}
TreeMap after reverse:{4=neveropen, 3=on, 2=geeks, 1=Hello}

 

Time Complexity: O(N)

 

RELATED ARTICLES

Most Popular

Recent Comments