Open In App

How to sort TreeSet in descending order in Java?

Last Updated : 22 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Java




// Java Program to print TreeSet in reverse Order
import java.util.TreeSet;
 
public class TreeSetDescending
{
 
    public static void main(String[] args)
    {
        // Declare a treeset
        TreeSet<Object> ints = new TreeSet<Object>();
        ints.add(2);
        ints.add(20);
        ints.add(10);
        ints.add(5);
        ints.add(7);
        ints.add(3);
 
        // Initialize treeset with
        // predefined set in reverse order
        // using descendingSet()
        TreeSet<Object> intsReverse =
            (TreeSet<Object>)ints.descendingSet();
 
        // Print the set
        System.out.println("Without descendingSet(): " +
                                                 ints);
        System.out.println("With descendingSet(): " +
                                           intsReverse);
    }
}


Output

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

Previous Article
Next Article

Similar Reads

How to Change the Comparator to Return a Descending Order in Java TreeSet?
Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ord
3 min read
Sort first k values in ascending order and remaining n-k values in descending order
Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order. Examples: Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 Output: 2 4 5 6 9 8 3 1 -1 Input: arr[] = {5, 4, 6}, k = 2 Output: 4 5 6 Algorithm: Store the first k elements in an array and sort that in ascending
11 min read
Sort Java Vector in Descending Order Using Comparator
The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface. There are two types of Sorting technique: First is internal sorting i.e that uses
3 min read
How to sort an ArrayList in Descending Order in Java
Given an unsorted ArrayList, the task is to sort this ArrayList in descending order in Java. Examples: Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Output: Sorted ArrayList: [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal] Input: Unsorted ArrayList: [Geeks, For, ForGeeks] Output: Sorted ArrayList: [Gee
2 min read
Sort ArrayList in Descending Order Using Comparator in Java
A comparator is an interface that is used to rearrange the ArrayList in a sorted manner. A comparator is used to sort an ArrayList of User-defined objects. In java, a Comparator is provided in java.util package. Using Comparator sort ArrayList on the basis of multiple variables, or simply implement Comparator without affecting the original User-def
3 min read
How to Sort LinkedHashSet Elements in Descending Order in Java?
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet iteration is through the elements in the order in which they were inserted. When cyclin
2 min read
Java Program to Sort the Array Elements in Descending Order
Sort the given array in descending order, i.e., arrange the elements from largest to smallest. Example: Input: Array = {2, 6, 23, 98, 24, 35, 78} Output: [98, 78, 35, 24, 23, 6, 2] Input: Array = {1, 2, 3, 4, 5} Output: [5, 4, 3, 2, 1]Sorting is a process of arranging items systematically. sort() is an inbuilt function from java.util.Arrays which i
3 min read
Sort first half in ascending and second half in descending order | Set 2
Given an array of integers, sort the first half of the array in ascending order and the second half in descending order. Examples: Input : arr[] = {10, 20, 30, 40} Output : arr[] = {10, 20, 40, 30} Input : arr[] = {5, 4, 6, 2, 1, 3, 8, 9, 7 } Output : arr[] = {2, 4, 5, 6, 9, 8, 7, 3, 1 } We have discussed a solution that only prints the required or
7 min read
Reversing Elements Order in TreeSet in Java Using the descendingIterator Method
The descendingIterator() method of java.util.TreeSet&lt;E&gt; class is used to return an iterator over the elements in the set in descending order. Once the iterator assigns with the return value of the descendingIterator(), iterate the iterator using while loop. Example: Input : TreeSet = [2, 5, 6] Output: Reverse = [6, 5, 2] Input : TreeSet = [a,
2 min read
How to Iterate Over the Elements in a TreeSet in Natural Order in Java?
In Java, to iterate over the elements of a TreeSet in their Natural Order, one must either use a custom comparator provided or traverse the elements in ascending order based on their natural ordering. A TreeSet in Java keeps up with its components in arranged requests. In this article, we will learn how to iterate over the elements in a TreeSet in
2 min read