📜  Java中的 TreeMap 降序映射()方法及示例

📅  最后修改于: 2022-05-13 01:54:42.757000             🧑  作者: Mango

Java中的 TreeMap 降序映射()方法及示例

DescingMap()方法用于返回此映射中包含的映射的逆序视图。映射的倒序或降序按照键的降序排列。降序映射由该映射支持,因此对映射的更改反映在降序映射中,反之亦然。

声明语法:

public NavigableMap descendingMap()
  • K :它是此映射维护的密钥类型。
  • V :它是映射值的类型。

参数:不可用

返回值:此地图的逆序或降序视图。

示例 1:

Java
// Java program to demonstrate descendingMap() method
  
import java.util.*;
  
public class Example1 {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap treemap
            = new TreeMap();
  
        // Add the mappings to the tree map using put()
        treemap.put(2, "Two");
        treemap.put(16, "Sixteen");
        treemap.put(8, "Eight");
        treemap.put(6, "Six");
        treemap.put(10, "Ten");
  
        // store the descending order of mappings in dmap
        NavigableMap dmap = treemap.descendingMap();
  
        System.out.println("Reverse navigable map values: "
                           + dmap);
    }
}


Java
// Java Program to demonstrate descendingMap() method
  
import java.util.*;
  
public class Example2 {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap treemap
            = new TreeMap();
  
        // Add the mappings to the tree map using put()
        treemap.put(11, "Abhishek Rout");
        treemap.put(9, "Akash Salvi");
        treemap.put(2, "Hemant Koul");
        treemap.put(8, "Vaibhav Kamble");
        treemap.put(6, "Sagar Joshi");
        treemap.put(10, "Onkar Dherange");
        treemap.put(7, "Rajwardhan Shinde");
        treemap.put(1, "Rahul Gavhane");
        treemap.put(4, "Abhishek Gadge");
        treemap.put(3, "Pratik Kulkarni");
        treemap.put(5, "Raviraj Bugge");
  
        // store the descending order of mappings in dmap
        NavigableMap dmap = treemap.descendingMap();
  
        // print the mappings
        System.out.println(
            "List of students in reverse order: " + dmap);
    }
}


输出
Reverse navigable map values: {16=Sixteen, 10=Ten, 8=Eight, 6=Six, 2=Two}


示例 2:

Java

// Java Program to demonstrate descendingMap() method
  
import java.util.*;
  
public class Example2 {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap treemap
            = new TreeMap();
  
        // Add the mappings to the tree map using put()
        treemap.put(11, "Abhishek Rout");
        treemap.put(9, "Akash Salvi");
        treemap.put(2, "Hemant Koul");
        treemap.put(8, "Vaibhav Kamble");
        treemap.put(6, "Sagar Joshi");
        treemap.put(10, "Onkar Dherange");
        treemap.put(7, "Rajwardhan Shinde");
        treemap.put(1, "Rahul Gavhane");
        treemap.put(4, "Abhishek Gadge");
        treemap.put(3, "Pratik Kulkarni");
        treemap.put(5, "Raviraj Bugge");
  
        // store the descending order of mappings in dmap
        NavigableMap dmap = treemap.descendingMap();
  
        // print the mappings
        System.out.println(
            "List of students in reverse order: " + dmap);
    }
}
输出
List of students in reverse order: {11=Abhishek Rout, 10=Onkar Dherange, 9=Akash Salvi, 8=Vaibhav Kamble, 7=Rajwardhan Shinde, 6=Sagar Joshi, 5=Raviraj Bugge, 4=Abhishek Gadge, 3=Pratik Kulkarni, 2=Hemant Koul, 1=Rahul Gavhane}