TreeMap navigableKeySet() in Java

navigableKeySet(): This method is available in java.util.TreeMap class of Java.

Syntax:

NavigableSet<K> java.util.TreeMap.navigableKeySet()

This method returns a NavigableSet view of the keys contained in this map. The set's iterator returns the keys in ascending order.

Note: The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Parameters: NA

Returns: a navigable set view of the keys in this map.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapnavigableKeySet {
    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        System.out.println(treeMap.navigableKeySet());

    }
}

Output:

[2, 6, 11, 23, 25]


No comments:

Post a Comment