Skip to content
Related Articles

Related Articles

TreeMap navigableKeySet() method in Java with Examples
  • Last Updated : 11 Oct, 2018
GeeksforGeeks - Summer Carnival Banner

The navigableKeySet() method of java.util.TreeMap class is used to return a NavigableSet view of the keys contained in this map.

The set’s iterator returns the keys in ascending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Syntax:

public NavigableSet navigableKeySet()

Return Value: This method returns a navigable set view of the keys in this map.

Below are the examples to illustrate the navigableKeySet() method



Example 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);
        }
    }
}
Output:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Value is: [1, 2, 3, 4, 5]

Example 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);
        }
    }
}
Output:
TreeMap: {A=1, B=2, C=3, D=4, E=5}
Value is: [A, B, C, D, E]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :