Java 逆序遍历TreeMap
TreeMap是Java集合框架的一个类,它实现了NavigableMap接口。它以树结构存储地图的元素,并提供了一种有效的方法来按排序顺序存储键值对。换句话说,它总是以升序返回元素。然而,Java提供了几种以降序遍历TreeMap的方式。在本文中,我们将探讨以逆序迭代TreeMap的方法。
在Java中逆序遍历TreeMap
我们将使用以下方法按逆序打印TreeMap的元素:
- 使用TreeMap.descendingMap()方法
-
使用TreeMap.descendingKeySet()方法
-
使用Collections.reverseOrder()方法
让我们逐个讨论它们的示例程序。
示例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
示例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
示例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()。