当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TreeMap descendingKeySet()用法及代码示例


TreeMap类的descendingKeySet()方法返回映射中包含的键的逆序NavigableSet视图。该集合的迭代器以降序返回键。

注意:该集受Map支持,因此对Map的更改会反映在该集内,反之亦然。

用法:

public NavigableSet<K> descendingKeySet()

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

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



异常:该方法不会引发任何异常。

范例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<Integer, String> treemap 
            = new TreeMap<Integer, String>(); 
  
        // 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); 
    } 
}

输出:

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<Integer, String> geeks 
            = new TreeMap<Integer, String>(); 
  
        // 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]




相关用法


注:本文由纯净天空筛选整理自ashishguru9803大神的英文原创作品 TreeMap descendingKeySet() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。