📜  Java中的TreeMap descendingKeySet() 方法及示例

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

Java中的TreeMap descendingKeySet() 方法及示例

TreeMap 类descendingKeySet()方法返回包含在地图中的键的逆序NavigableSet视图。集合的迭代器按降序返回键。

注意:集合由地图支持,因此对地图的更改会反映在集合内,反之亦然。

句法:

public NavigableSet descendingKeySet()

参数:该方法不带任何参数。

返回值:该方法返回地图中包含的值的可导航集合视图。

异常:该方法不抛出任何异常。

示例 1:

Java
// Java Program to show the working
// of descendingKeySet() Method
import java.io.*;
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // creating tree map of Integer and String
        TreeMap treemap
            = new TreeMap();
  
        // populating tree map using put()
        treemap.put(3, "three");
        treemap.put(1, "one");
        treemap.put(2, "two");
        treemap.put(0, "zero");
        treemap.put(7, "seven");
        treemap.put(6, "six");
  
        // putting values in navigable set
        // use of descendingKeySet
        NavigableSet set1 = treemap.descendingKeySet();
  
        System.out.println("Navigable set values are: "
                           + set1);
    }
}


Java
// Java Program to show the working
// of descendingKeySet() Method
import java.io.*;
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // creating tree map of Integer and String
        TreeMap geeks
            = new TreeMap();
  
        // putting values in navigable set
        geeks.put(1, "Guru");
        geeks.put(2, "Ayush");
        geeks.put(3, "Devesh");
        geeks.put(4, "Kashish");
  
        System.out.println("TreeMap values :- " + geeks);
  
        // use of descendingKeySet
        NavigableSet nevigableSet
            = geeks.descendingKeySet();
  
        System.out.println("Reverse key values:- "
                           + nevigableSet);
    }
}


输出:

Navigable set values are: [7, 6, 3, 2, 1, 0]

示例 2:

Java

// Java Program to show the working
// of descendingKeySet() Method
import java.io.*;
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // creating tree map of Integer and String
        TreeMap geeks
            = new TreeMap();
  
        // putting values in navigable set
        geeks.put(1, "Guru");
        geeks.put(2, "Ayush");
        geeks.put(3, "Devesh");
        geeks.put(4, "Kashish");
  
        System.out.println("TreeMap values :- " + geeks);
  
        // use of descendingKeySet
        NavigableSet nevigableSet
            = geeks.descendingKeySet();
  
        System.out.println("Reverse key values:- "
                           + nevigableSet);
    }
}

输出:

TreeMap values :- {1=Guru, 2=Ayush, 3=Devesh, 4=Kashish}
Reverse key values:- [4, 3, 2, 1]