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

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

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

Java.util.TreeMap类的navigableKeySet()方法用于返回此映射中包含的键的 NavigableSet 视图。
集合的迭代器按升序返回键。集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。如果在对集合进行迭代时修改了映射(通过迭代器自己的删除操作除外),则迭代的结果是不确定的。该集合支持元素移除,即通过 Iterator.remove、Set.remove、removeAll、retainAll 和 clear 操作从映射中移除相应的映射。它不支持 add 或 addAll 操作。
句法:

public NavigableSet navigableKeySet()

返回值:此方法返回此地图中键的可导航集合视图
以下是说明navigableKeySet()方法的示例
示例 1:

Java
// 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
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting navigable set view of the keys
            // using navigableKeySet() method
            NavigableSet
                value = treemap.navigableKeySet();
 
            // printing the value
            System.out.println("Value is: " + value);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// 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
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put("A", 1);
            treemap.put("B", 2);
            treemap.put("C", 3);
            treemap.put("D", 4);
            treemap.put("E", 5);
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting navigable set view of the keys
            // using navigableKeySet() method
            NavigableSet
                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

// 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
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put("A", 1);
            treemap.put("B", 2);
            treemap.put("C", 3);
            treemap.put("D", 4);
            treemap.put("E", 5);
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting navigable set view of the keys
            // using navigableKeySet() method
            NavigableSet
                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]