Home » Java programming language

Java TreeSet descendingSet() Method with Example

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

TreeSet Class descendingSet() method

  • descendingSet() method is available in java.util package.
  • descendingSet() method is used to get the elements of this TreeSet in reverse order.
  • descendingSet() 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.
  • descendingSet() method does not throw an exception at the time of returning the navigable set.

Syntax:

    public NavigableSet descendingSet();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is NavigableSet, it gets this TreeSet elements in reverse order.

Example:

// Java program to demonstrate the example 
// of NavigableSet descendingSet() method of TreeSet 

import java.util.*;

public class DescendingSetOfTreeSet {
    public static void main(String[] args) {
        // Instantiates a TreeSet object
        TreeSet < String > tree_set = new TreeSet < String > ();
        TreeSet < String > rev_set = new TreeSet < String > ();

        // By using add() method is to add
        // the given object of this TreeSet 
        tree_set.add("C");
        tree_set.add("C++");
        tree_set.add("JAVA");
        tree_set.add("PHP");
        tree_set.add("SFDC");

        // Display TreeSet
        System.out.println("TreeSet: " + tree_set);

        // By using descendingSet() method is to 
        // return the elements of TreeSet in descending 
        // order to be viewed in a set
        rev_set = (TreeSet) tree_set.descendingSet();

        // Iterating elements
        System.out.println("rev_set.descendingSet(): ");
        for (Iterator itr = rev_set.iterator(); itr.hasNext();)
            System.out.println(itr.next());
    }
}

Output

TreeSet: [C, C++, JAVA, PHP, SFDC]
rev_set.descendingSet(): 
SFDC
PHP
JAVA
C++
C


Comments and Discussions!

Load comments ↻





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