如何在 Java 中以逆序建立 TreeMap?
在本文中,我們將學習如何在 Java 中以逆序建立 TreeMap。首先,我們需要了解 **TreeMap**。Java 中的 **TreeMap** 是一個實現 **SortedMap** 介面的類,該介面擴充套件了 Map 介面。它是一個儲存鍵值對並根據其自然順序或建立過程中提供的自定義 Comparator 對其進行組織的集合。
這種方法為新增、刪除和從 TreeMap 中檢索元素等常見操作提供了 **高效的效能**。它具有 **平均時間複雜度** **O(log n)**,確保了高效的執行。
建立 TreeMap 的語法
TreeMap(Map<? extends K,? extends V> m)
此方法用於透過新增來自 'm' 的條目來初始化 TreeMap。條目將根據鍵的自然順序進行排序。
方法
**TreeMap** 的預設行為是根據鍵按升序對元素進行排序。但是,透過使用 Java 中的 **Collections.reverseOrder()** 方法,我們可以建立一個以 **逆序** 維護元素的 TreeMap,使我們能夠根據鍵按降序顯示它們。
Collections.reverseOrder()
**reverseOrder()** 方法返回一個 Comparator,可用於根據給定的 Comparator 以逆序對集合或列表進行排序。此方法允許我們使用自定義比較器輕鬆地將元素排列為降序。
語法
public static Comparator reverseOrder()
輸入
tm.put("1", "Welcome");
tm.put("2", "to");
tm.put("3", "the");
tm.put("4", "Tutorials");
tm.put("5", "Point");
輸出
// The goal is to print the elements in the reverse order of when they were inserted 5: Point 4: Tutorials 3: the 2: to 1: Welcome
示例
//The following code demonstrates how to iterate through a TreeMap in reverse order in Java.
import java.util.*;
public class Testing {
public static void main(String args[]){
//Creating a tree Map
Map<String, String> tm =
new TreeMap<String, String>(Collections.reverseOrder());
// Use put() method to insert elements into a treemap
tm.put("1", "Welcome");
tm.put("2", "to");
tm.put("3", "the");
tm.put("4", "Tutorials");
tm.put("5", "Point");
// Traversing and printing the elements in map
for(Map.Entry<String,String> me: tm.entrySet()){
System.out.println(me.getKey() + " : " + me.getValue());
}
}
}
輸出
5 : Point 4 : Tutorials 3 : the 2 : to 1 : Welcome
示例中的元素根據其鍵以逆序顯示。
在這個例子中,我們建立了一個名為 Person 的類,它包含兩個屬性,例如姓名和年齡。在這裡,我們將建立一個 TreeMap,其中鍵為 Integer,值為 Person 物件,並以逆序顯示它們。
輸入
treeMap.put(1, new Person("Hari", 25));
treeMap.put(2, new Person("Revanth", 30));
treeMap.put(3, new Person("Mohan", 35));
t
輸出
David (40 years old) => Key: 4 Charlie (35 years old) => Key: 3 Bob (30 years old) => Key: 2 Alice (25 years old) => Key: 1
下面的程式幫助我們遍歷上述示例中的 TreeMap 以實現逆序遍歷 -
示例
//The code snippet below illustrates how to traverse a TreeMap in reverse order using Java.
import java.util.*;
class PersonDetails {
private String person_name;
private int person_age;
public PersonDetails(String name, int age) {
this.person_name = name;
this.person_age = age;
}
public String getName() {
return person_name;
}
public int getAge() {
return person_age;
}
@Override
public String toString() {
return person_name + " (" + person_age + " years old)";
}
}
public class Testing {
public static void main(String[] args) {
// Create a TreeMap with Integer as Key and value as Person(Name,age)
TreeMap<Integer, PersonDetails> tm = new TreeMap<>(Collections.reverseOrder());
// Use put() method to insert elements into a treemap
tm.put(1, new PersonDetails("Alice", 25));
tm.put(2, new PersonDetails("Bob", 30));
tm.put(3, new PersonDetails("Charlie", 35));
tm.put(4, new PersonDetails("David", 40));
// Traversing and printing the elements in map
for (Map.Entry<Integer, PersonDetails> me : tm.entrySet()) {
System.out.println(me.getValue() + " => Key: " + me.getKey());
}
}
}
輸出
David (40 years old) => Key: 4 Charlie (35 years old) => Key: 3 Bob (30 years old) => Key: 2 Alice (25 years old) => Key: 1
在這個例子中,我們建立了一個名為 Product 的類,它包含兩個屬性,例如名稱和價格。在這裡,我們將建立一個 TreeMap,其中鍵為 product_name,值為 product_price,並以逆序顯示它們。
輸入
tm.put("Product A", 9.99);
tm.put("Product B", 5.99);
tm.put("Product C", 12.49);
tm.put("Product D", 7.99);
tm.put("Product E", 3.99);
輸出
Product E => $3.99 Product D => $7.99 Product C => $12.49 Product B => $5.99 Product A => $9.99
示例
//The code snippet below illustrates how to traverse a TreeMap in reverse order using Java.
import java.util.*;
class ProductDetails {
private String product_name;
private double product_price;
public ProductDetails(String name, double price) {
this.product_name = name;
this.product_price = price;
}
public String getName() {
return product_name;
}
public double getPrice() {
return product_price;
}
@Override
public String toString() {
return product_name + " ($" + product_price + ")";
}
}
public class Testing {
public static void main(String[] args) {
// Create a TreeMap with key as product_name and value as product_price
TreeMap<String, Double> tm= new TreeMap<>(Collections.reverseOrder());
// Use put() method to insert elements into a treemap
tm.put("Product A", 9.99);
tm.put("Product B", 5.99);
tm.put("Product C", 12.49);
tm.put("Product D", 7.99);
tm.put("Product E", 3.99);
// Print the TreeMap
for (Map.Entry<String, Double> me : tm.entrySet()) {
System.out.println(me.getKey() + " => $" + me.getValue());
}
}
}
輸出
Product E => $3.99 Product D => $7.99 Product C => $12.49 Product B => $5.99 Product A => $9.99
結論
在本文中,我們探討了透過使用 **reverseOrder()** 方法以逆序建立 TreeMap 的過程,並且我們已經看到了幾個如何使用此 reverseOder() 方法來執行此任務的示例。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP