Given a TreeSet in Java, task is to sort elements of TreeSet in Descending Order (descreasing order).
Input : Set: [2, 3, 5, 7, 10, 20] Output : Set: [20, 10, 7, 5, 3, 2] Input : Set: [computer, for, geeks, hello] Output : Set: [hello, geeks, for, computer]
To make a TreeSet Element in decreasing order, simple use descendingSet() method which is used to change the order of TreeSet in reverse order
Below is the implementation of the above approach:
Without descendingSet(): [2, 3, 5, 7, 10, 20] With descendingSet(): [20, 10, 7, 5, 3, 2]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to sort an ArrayList in Descending Order in Java
- Java Program to Sort the Elements of an Array in Descending Order
- Sort first half in ascending and second half in descending order | Set 2
- Different ways to sort an array in descending order in C#
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Number of pairs of arrays (A, B) such that A is ascending, B is descending and A[i] ≤ B[i]
- TreeSet in Java
- TreeSet in Java
- TreeSet size() Method in Java
- TreeSet iterator() Method in Java
- TreeSet remove() Method in Java
- TreeSet pollFirst() method in Java
- Job Sequencing Problem | Set 3 (Using TreeSet in JAVA)
- TreeSet subSet() Method in Java
- TreeSet tailSet() Method in Java
- TreeSet headSet() Method in Java
- TreeSet last() Method in Java
- TreeSet first() Method in Java
- TreeSet isEmpty() Method in Java
- TreeSet contains() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : mayur kumbhar