Home  >  Article  >  Java  >  Iterate over TreeMap in reverse order in Java

Iterate over TreeMap in reverse order in Java

WBOY
WBOYforward
2023-08-21 13:05:061671browse

Iterate over TreeMap in reverse order in Java

TreeMap is a class of Java Collection Framework that implements the NavigableMap interface. It stores the map's elements in a tree structure and provides an efficient way to store key-value pairs in sorted order. In other words, it always returns elements in ascending order. However, Java provides several methods for traversing a TreeMap in descending order. In this article, we will explore the method of traversing a TreeMap in reverse order.

Iterate over TreeMap in reverse order in Java

We will print the elements of the TreeMap in reverse order using the following method:

  • Use TreeMap.descendingMap() method

  • Use TreeMap.descendingKeySet() method

  • Use Collections.reverseOrder() method

Let us discuss them one by one with example programs

The Chinese translation of

Example 1

is:

Example 1

In this example, we will use the built-in method TreeMap.descendingMap() to iterate the TreeMap in reverse order. To do this, we first define a TreeMap and then store its elements in reverse order into another map.

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
The Chinese translation of

Example 2

is:

Example 2

In the following example, we will use the built-in method TreeMap.descendingKeySet() to iterate through the TreeMap in reverse order. For this operation, instead of creating a Map like in the previous example, we create a collection that stores the Map keys 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
The Chinese translation of

Example 3

is:

Example 3

This is another example of getting TreeMap elements in reverse order. We just need to pass the Collections.reverseOrder() method to the constructor of TreeMap and it will return the elements of the 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

in conclusion

We start by defining a TreeMap. In the next section, we discuss how to traverse the TreeMap in reverse order. To do this we use three different built-in methods: descendingMap(), descendingKeySet() and Collections.reverseOrder()

The above is the detailed content of Iterate over TreeMap in reverse order in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete