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()
以上是在Java中以相反顺序迭代TreeMap的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用