TreeMap是Java Collection Framework的一個類,它實作了NavigableMap介面。它將地圖的元素儲存在樹狀結構中,並提供了一種有效的方法來按排序順序儲存鍵值對。換句話說,它總是以升序返回元素。然而,Java提供了幾種以降序遍歷TreeMap的方法。在本文中,我們將探討以逆序遍歷TreeMap的方法。
在Java中以相反順序迭代TreeMap
我們將使用以下方法以相反的順序列印TreeMap的元素:
讓我們透過範例程式逐一討論它們
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() ###以上是在Java中以相反順序迭代TreeMap的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java在不同操作系統上的表現存在細微差異。 1)JVM實現不同,如HotSpot、OpenJDK,影響性能和垃圾回收。 2)文件系統結構和路徑分隔符不同,需使用Java標準庫處理。 3)網絡協議實現差異影響網絡性能。 4)GUI組件外觀和行為在不同系統上有別。通過使用標準庫和虛擬機測試,可減少這些差異的影響,確保Java程序穩定運行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,對象,繼承,多態性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允許theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

java'splatformendependecemeansapplicationscanrunonanyplatformwithajvm,使“ Writeonce,runanywhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。