- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate TreeMap in Reverse Order in Java
TreeMap is a class of Java Collection Framework that implements NavigableMap Interface. It stores the elements of the map in a tree structure and provides an efficient alternative to store the key-value pairs in sorted order. In other words, it always returns the elements in ascending order. However, Java provides a few ways to iterate through the TreeMap in descending order. In this article, we are going to explore the ways of iterating TreeMap in Reverse order.
Iterate TreeMap in Reverse Order in Java
We will use the following ways to print elements of TreeMap in reverse order:
Using TreeMap.descendingMap() method
Using TreeMap.descendingKeySet() method
Using Collections.reverseOrder() method
Let's discuss them one by one through example programs.
Example 1
In this example, we will use the built-in method TreeMap.descendingMap() to iterate TreeMap in reverse order. For this, we will define a TreeMap first and then, store its elements into another map in reverse order.
import java.util.*; public class Example1 { public static void main(String[] args) { // creating a TreeMap TreeMap<String, Integer> TrMap = new TreeMap<>(); // Adding elements in the map TrMap.put("Backpack", 4000); TrMap.put("Desktop", 3000); TrMap.put("Keypad", 1500); TrMap.put("Watch", 2000); TrMap.put("Pen drive", 2500); // storing the elements of the map in descending order Map<String, Integer> newMap = TrMap.descendingMap(); // printing the details of map System.out.println("Elements of the map in Reverse Order: "); // iterating through the map for (String unKey : newMap.keySet()) { // printing details of map in reverse order System.out.println("Item: " + unKey + ", Price: " + newMap.get(unKey)); } } }
Output
Elements of the map in Reverse Order: Item: Watch, Price: 2000 Item: Pen drive, Price: 2500 Item: Keypad, Price: 1500 Item: Desktop, Price: 3000 Item: Backpack, Price: 4000
Example 2
In the following example, we will use the built-in method TreeMap.descendingKeySet() to iterate through TreeMap in reverse order. For this operation, instead of creating a Map like we did in the previous example, we will create a set that will store the keys of map in reverse order. Furthermore, using these keys we will get the corresponding values.
import java.util.*; public class Example2 { public static void main(String[] args) { // creating a TreeMap TreeMap<Integer, String> TrMap = new TreeMap<>(); // Adding elements in the map TrMap.put(40, "Backpack"); TrMap.put(12, "Desktop"); TrMap.put(150, "Keypad"); TrMap.put(125, "Watch"); TrMap.put(250, "Pen drive"); // retrieving the keys in reverse order Set<Integer> keys = TrMap.descendingKeySet(); // printing the details of map System.out.println("Elements of the map in Reverse Order: "); // iterating through the map for (Integer unKey : keys) { // printing details of map in reverse order System.out.println("Item: " + TrMap.get(unKey) + ", Quantity: " + unKey); } } }
Output
Elements of the map in Reverse Order: Item: Pen drive, Quantity: 250 Item: Keypad, Quantity: 150 Item: Watch, Quantity: 125 Item: Backpack, Quantity: 40 Item: Desktop, Quantity: 12
Example 3
This is another example of getting elements of a TreeMap in reverse order. We simply need to pass the Collections.reverseOrder() method to the constructor of TreeMap that will return the elements of TreeMap collection in reverse order.
import java.util.*; public class Example3 { public static void main(String[] args) { // creating a TreeMap by passing Collections.reverseOrder() TreeMap<String, Integer> TrMap = new TreeMap<>(Collections.reverseOrder()); // Adding elements in the map TrMap.put("Kurti", 4000); TrMap.put("Shirt", 3000); TrMap.put("TShirt", 1500); TrMap.put("Watch", 2000); TrMap.put("Perfume", 2500); // printing the details of map System.out.println("Elements of the map in Reverse Order: "); // iterating through the map for (String unKey : TrMap.keySet()) { // printing details of map in reverse order System.out.println("Item: " + unKey + ", Price: " + TrMap.get(unKey)); } } }
Output
Elements of the map in Reverse Order: Item: Watch, Price: 2000 Item: TShirt, Price: 1500 Item: Shirt, Price: 3000 Item: Perfume, Price: 2500 Item: Kurti, Price: 4000
Conclusion
We started this article by defining the TreeMap and in the next section, we discussed how to iterate through the TreeMap in reverse order. For this operation, we have used three different built-in methods: descendingMap(), descendingKeySet() and Collections.reverseOrder().