首页 > 文章列表 > 在Java中以相反顺序迭代TreeMap

在Java中以相反顺序迭代TreeMap

java 迭代 TreeMap
356 2023-08-23

TreeMap是Java Collection Framework的一个类,它实现了NavigableMap接口。它将地图的元素存储在树结构中,并提供了一种有效的方法来按排序顺序存储键值对。换句话说,它总是以升序返回元素。然而,Java提供了几种以降序遍历TreeMap的方法。在本文中,我们将探讨以逆序遍历TreeMap的方法。

在Java中以相反顺序迭代TreeMap

我们将使用以下方法以相反的顺序打印TreeMap的元素:

  • 使用TreeMap.descendingMap()方法

  • 使用TreeMap.descendingKeySet()方法

  • 使用 Collections.reverseOrder() 方法

让我们通过示例程序逐一讨论它们

Example 1

的中文翻译为:

示例 1

在这个例子中,我们将使用内置的方法TreeMap.descendingMap()来以相反的顺序迭代TreeMap。为此,我们首先定义一个TreeMap,然后将其元素按相反的顺序存储到另一个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));
      }
   }
}

输出

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

的中文翻译为:

示例2

在下面的示例中,我们将使用内置的方法TreeMap.descendingKeySet()来以相反的顺序迭代遍历TreeMap。对于这个操作,我们不再创建一个像前面示例中那样的Map,而是创建一个以相反顺序存储Map键的集合。此外,使用这些键我们将获取相应的值。

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);
      }
   }
}

输出

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

的中文翻译为:

示例3

这是另一个获取TreeMap元素按相反顺序的示例。我们只需要将Collections.reverseOrder()方法传递给TreeMap的构造函数,它将以相反顺序返回TreeMap集合的元素。

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));
      }
   }
}

输出

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

结论

我们从定义TreeMap开始,接下来的部分中,我们讨论了如何按照相反的顺序遍历TreeMap。为了进行这个操作,我们使用了三种不同的内置方法:descendingMap(),descendingKeySet()和Collections.reverseOrder()