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:
- Using the reverseOrder() method
- Using the descendingKeySet() method
- 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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> treeMap
= new TreeMap<>(Collections.reverseOrder());
treeMap.put( 1 , "Hello" );
treeMap.put( 2 , "geeks" );
treeMap.put( 3 , "on" );
treeMap.put( 4 , "geeksforgeeks" );
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put( 1 , "Hello" );
treeMap.put( 2 , "geeks" );
treeMap.put( 3 , "on" );
treeMap.put( 4 , "geeksforgeeks" );
System.out.println( "TreeMap before reverse: "
+ treeMap);
Set<Integer> keySet = treeMap.descendingKeySet();
System.out.println( "TreeMap after reverse:" );
for (Integer key : keySet) {
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put( 1 , "Hello" );
treeMap.put( 2 , "geeks" );
treeMap.put( 3 , "on" );
treeMap.put( 4 , "geeksforgeeks" );
System.out.println( "TreeMap before reverse:"
+ treeMap);
Map<Integer, String> reverseMap
= treeMap.descendingMap();
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)
Last Updated :
24 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...