java.util.TreeMap類的navigableKeySet()方法用於返回此映射中包含的鍵的NavigableSet視圖。
集合的迭代器以升序返回 key 。該集合由Map支持,因此對Map的更改會反映在集合中,反之亦然。如果在對集合進行迭代時修改了Map(通過迭代器自己的remove操作除外),則迭代的結果是不確定的。該集合支持元素刪除,該元素通過Iterator.remove,Set.remove,removeAll,retainAll和clear操作從映射中刪除相應的映射。它不支持add或addAll操作。
用法:
public NavigableSet navigableKeySet()
返回值:此方法返回此映射中的鍵的可導航設置視圖。
以下示例說明了navigableKeySet()方法
示例1:
// Java program to demonstrate
// navigableKeySet() method
// for Integer value key
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of TreeMap<Integer, String>
TreeMap<Integer, String>
treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(1, "One");
treemap.put(2, "Two");
treemap.put(3, "Three");
treemap.put(4, "Four");
treemap.put(5, "Five");
// pritnig the TreeMap
System.out.println("TreeMap: " + treemap);
// getting navigable set view of the keys
// using navigableKeySet() method
NavigableSet<Integer>
value = treemap.navigableKeySet();
// printing the value
System.out.println("Value is: " + value);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five} Value is: [1, 2, 3, 4, 5]
示例2:
// Java program to demonstrate
// navigableKeySet() method
// for String value key
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of TreeMap<Integer, String>
TreeMap<String, Integer>
treemap = new TreeMap<String, Integer>();
// populating tree map
treemap.put("A", 1);
treemap.put("B", 2);
treemap.put("C", 3);
treemap.put("D", 4);
treemap.put("E", 5);
// pritnig the TreeMap
System.out.println("TreeMap: " + treemap);
// getting navigable set view of the keys
// using navigableKeySet() method
NavigableSet<String>
value = treemap.navigableKeySet();
// printing the value
System.out.println("Value is: " + value);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
TreeMap: {A=1, B=2, C=3, D=4, E=5} Value is: [A, B, C, D, E]
相關用法
- Java TreeMap higherKey()用法及代碼示例
- Java TreeMap lowerEntry()用法及代碼示例
- Java TreeMap comparator()用法及代碼示例
- Java TreeMap higherEntry()用法及代碼示例
- Java TreeMap ceilingKey()用法及代碼示例
- Java TreeMap floorKey()用法及代碼示例
- Java TreeMap lowerKey()用法及代碼示例
- Java TreeMap put()用法及代碼示例
- Java TreeMap get()用法及代碼示例
- Java TreeMap headMap()用法及代碼示例
- Java TreeMap entrySet()用法及代碼示例
- Java TreeMap putAll()用法及代碼示例
- Java TreeMap remove()用法及代碼示例
- Java TreeMap values()用法及代碼示例
- Java TreeMap subMap()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 TreeMap navigableKeySet() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。