在Java中以相反顺序迭代TreeMap

2024-03-04 0 3,086

TreeMapJava 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()

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 在Java中以相反顺序迭代TreeMap http://www.20zxx.cn/809076/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务