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 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]
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); } } } |
TreeMap: {A=1, B=2, C=3, D=4, E=5}
Value is: [A, B, C, D, E]
Recommended Posts:
- TreeMap higherEntry() method in Java with Examples
- TreeMap higherKey() method in Java with Examples
- TreeMap lowerEntry() method in Java with Examples
- TreeMap comparator() method in Java with Examples
- TreeMap ceilingKey() in Java with Examples
- TreeMap lowerKey() in Java with Examples
- TreeMap floorKey() in Java with Examples
- TreeMap get() Method in Java
- TreeMap put() Method in Java
- TreeMap size() Method in Java
- TreeMap containsValue() Method in Java
- TreeMap clone() Method in Java
- TreeMap putAll() Method in Java
- TreeMap lastKey() Method in Java
- TreeMap keySet() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



