TreeSet descendingSet() method in Java with Examples

The descendingSet() method of java.util.TreeSet<E> class is used to return a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iteration over either set is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined.

The returned set has an ordering equivalent to Collections.reverseOrder(comparator()). The expression s.descendingSet().descendingSet() returns a view of s essentially equivalent to s.

Syntax:

public NavigableSet descendingSet()

Return Value: This method returns a reverse order view of this set.

Below are the examples to illustrate the descendingSet() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// descendingSet() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // create tree set object
            TreeSet<String> treeadd = new TreeSet<String>();
  
            // populate the TreeSet using add() method
            treeadd.add("A");
            treeadd.add("B");
            treeadd.add("C");
            treeadd.add("D");
  
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
  
            // getting the reverse order view of element
            // using descendingSet() method
            NavigableSet<String>
                treereverse = treeadd.descendingSet();
  
            // getting iterated view of NavigableSet
            Iterator<String> iterator = treereverse.iterator();
  
            System.out.println("\nValues using DescendingSet:");
  
            // printing the interated value
            while (iterator.hasNext()) {
                System.out.println("Value : "
                                   + iterator.next());
            }
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

TreeSet: [A, B, C, D]

Values using DescendingSet:
Value : D
Value : C
Value : B
Value : A

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// descendingSet() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // create tree set object
            TreeSet<Integer> treeadd = new TreeSet<Integer>();
  
            // populate the TreeSet using add() method
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
  
            // getting the reverse order view of element
            // using descendingSet() method
            NavigableSet<Integer> treereverse = treeadd.descendingSet();
  
            // getting iterated view of NavigableSet
            Iterator<Integer> iterator = treereverse.iterator();
  
            System.out.println("\nValues using DescendingSet:");
  
            // printing the interated value
            while (iterator.hasNext()) {
                System.out.println("Value : " + iterator.next());
            }
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Values using DescendingSet:
Value : 40
Value : 30
Value : 20
Value : 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.