TreeSet descendingSet() in Java

descendingSet(): This method is available in java.util.TreeSet class of Java.

Syntax:

NavigableSet<K> java.util.TreeSet.descendingSet()

This method returns a reverse order view of the elements contained in this set.

Note: The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa.

Parameters: NA

Returns: a reverse order view of this set.

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetdescendingSet {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.descendingSet());

    }
}

Output:

[Program, Java, Hello, C++]


No comments:

Post a Comment