Home » Java programming language

Java TreeMap descendingKeySet() Method with Example

TreeMap Class descendingKeySet() method: Here, we are going to learn about the descendingKeySet() method of TreeMap Class with its syntax and example.
Submitted by Preeti Jain, on February 19, 2020

TreeMap Class descendingKeySet() method

  • descendingKeySet() method is available in java.util package.
  • descendingKeySet() method is used to return the reverse order of keys that exist in this TreeMap to be viewed in a NavigableSet.
  • descendingKeySet() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • descendingKeySet() method does not throw an exception at the time of returning the navigable set.

Syntax:

    public NavigableSet descendingKeySet();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is NavigableSet, it returns the descending order of keys exists in this map to be viewed in a NavigableSet.

Example:

// Java program to demonstrate the example 
// of NavigableSet descendingKeySet() method of TreeMap

import java.util.*;

public class DescendingKeySetOfTreeMap {
    public static void main(String[] args) {
        // Instantiates a TreeMap object
        TreeMap < Integer, String > tree_map = new TreeMap < Integer, String > ();

        // By using put() method is to add
        // key-value pairs in a TreeMap
        tree_map.put(10, "C");
        tree_map.put(20, "C++");
        tree_map.put(50, "JAVA");
        tree_map.put(40, "PHP");
        tree_map.put(30, "SFDC");

        // Display TreeMap 
        System.out.println("TreeMap: " + tree_map);

        // By using descendingKeySet() method is to
        // order the keys in descending order to be
        // viewed in a NavigableSet
        NavigableSet ns = tree_map.descendingKeySet();

        // Display Status
        System.out.println("tree_map.descendingKeySet(): " + ns);
    }
}

Output

TreeMap: {10=C, 20=C++, 30=SFDC, 40=PHP, 50=JAVA}
tree_map.descendingKeySet(): [50, 40, 30, 20, 10]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.