TreeMap keySet() in Java

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

Syntax:

Set<K> java.util.TreeMap.keySet()

This method returns a Set view of the keys contained in this map.

Parameters: NA

Returns: a set view of the keys contained in this map, sorted in ascending order.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapkeySet {
    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.keySet());

    }
}

Output:

[2, 6, 11, 23, 25]


No comments:

Post a Comment