Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaTreeMap navigableKeySet() method in Java with Examples

TreeMap navigableKeySet() method in Java with Examples

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




// 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");
 
            // printing 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




// 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);
 
            // printing 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]

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments