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.
public NavigableSet descendingSet()
Return Value: This method returns a reverse order view of this set.
Below are the examples to illustrate the descendingSet() method
TreeSet: [A, B, C, D] Values using DescendingSet: Value : D Value : C Value : B Value : A
Values using DescendingSet: Value : 40 Value : 30 Value : 20 Value : 10
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.