VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-sort-treeset-in-descending-order-in-java/

⇱ How to sort TreeSet in descending order in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to sort TreeSet in descending order in Java?

Last Updated : 11 Jul, 2025

Given a TreeSet in Java, task is to sort elements of TreeSet in Descending Order (descreasing order).
Examples: 

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]

Approach: 
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: 


Output
Without descendingSet(): [2, 3, 5, 7, 10, 20]
With descendingSet(): [20, 10, 7, 5, 3, 2]
Comment