當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。