TreeMap descendingKeySet() in Java

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

Syntax:

NavigableSet<Integer> java.util.TreeMap.descendingKeySet()

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

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMapdescendingKeySet {
    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.descendingKeySet());
    }
}

Output:

[25, 23, 11, 6, 2]


No comments:

Post a Comment