This example shows how to reverse the order of the TreeSet elements in Java. The example also shows how to iterate the TreeSet in reverse order using the descendingSet or descendingIterator methods.
How to reverse the order of TreeSet elements in Java?
There are several ways using which we can reverse the order of the TreeSet elements or simply iterate the elements in reverse order in Java.
1. Using the descendingSet method (Java 1.6 and later)
Java version 1.6 introduced the descendingSet
method that returns a reverse order view of the elements contained in the TreeSet object.
1 |
public NavigableSet<E> descendingSet() |
Once we get the reverse set, we can iterate through it using an iterator as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class TreeSetReverseOrderExample { public static void main(String[] args) { TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); /* * To get the a reverse order view of this TreeSet, use * the descendingSet method */ Set<Integer> reverseSet = tSetOddNumbers.descendingSet(); //get an iterator Iterator<Integer> itr = reverseSet.iterator(); System.out.println("Reverse set contains: "); while(itr.hasNext()){ System.out.println( itr.next() ); } } } |
Output
1 2 3 4 5 6 |
TreeSet contains: [1, 3, 5, 7, 9] 9 7 5 3 1 |
Important Note: The reverse Set returned by the descendingSet
method is just a view and is backed by the original TreeSet object. So any changes you make to the original TreeSet object will be reflected in the reverse set, and vice versa.
2. Using the descendingIterator method (Java 1.6 and later versions)
Along with the descendingSet
method, Java 1.6 also introduced the descendingIterator
method.
1 |
public Iterator<E> descendingIterator() |
Instead of returning a set view, this method returns an iterator over the elements of the TreeSet in reverse order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); //get the reverse iterator Iterator<Integer> itr = tSetOddNumbers.descendingIterator(); System.out.println("Iterating TreeSet in reverse order"); while(itr.hasNext()){ System.out.println( itr.next() ); } |
Output
1 2 3 4 5 6 7 |
TreeSet contains: [1, 3, 5, 7, 9] Iterating TreeSet in reverse order 9 7 5 3 1 |
3. Using the reverseOrder method of the Collections class
The reverseOrder
of the Collections class returns a Comparator that reverses the natural ordering of the elements.
1 |
public static <T> Comparator<T> reverseOrder() |
The natural ordering of an object is defined by implementing the Comparable interface and defining the compareTo
method.
The below given example shows how to sort TreeSet elements in descending order or reverse order using this method.
1 2 3 4 5 6 7 8 9 10 11 12 |
/* * Create TreeSet using the descending order */ TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>( Collections.reverseOrder() ); tSetOddNumbers.add(7); tSetOddNumbers.add(3); tSetOddNumbers.add(9); tSetOddNumbers.add(5); tSetOddNumbers.add(1); System.out.println("TreeSet contains: " + tSetOddNumbers); |
Output
1 |
TreeSet contains: [9, 7, 5, 3, 1] |
As we can see from the output, the TreeSet object has sorted the elements in descending order.
Important Note:
The first two approaches did not change the order of the original TreeSet. The elements were still stored in the TreeSet in their natural ordering while this approach changes the order in which the elements are stored in the TreeSet object itself.
How to change the Comparator to return a descending order?
Suppose you already have a custom comparator that defines the TreeSet ordering and you want to change that to return the reverse order as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Comparator; import java.util.TreeSet; class Subject{ int subjectId; public Subject(int subjectId){ this.subjectId = subjectId; } public int getSubjectId(){ return this.subjectId; } public String toString(){ return "Subject -> " + getSubjectId(); } } class SubjectComparator implements Comparator<Subject>{ public int compare(Subject subject1, Subject subject2) { return subject1.getSubjectId() - subject2.getSubjectId(); } } public class TreeSetReverseOrderExample { public static void main(String[] args) { //TreeSet with a custom comparator TreeSet<Subject> tSetOddSubjects = new TreeSet<Subject>( new SubjectComparator() ); tSetOddSubjects.add( new Subject(102) ); tSetOddSubjects.add( new Subject(103) ); tSetOddSubjects.add( new Subject(101) ); System.out.println( "TreeSet contains: " + tSetOddSubjects ); } } |
Output
1 |
TreeSet contains: [Subject -> 101, Subject -> 102, Subject -> 103] |
We can reverse the order of the elements by simply flipping the arguments subject1 and subject2 as given below to return the elements in the descending order.
1 2 3 4 5 6 |
class SubjectComparator implements Comparator<Subject>{ public int compare(Subject subject1, Subject subject2) { return subject2.getSubjectId() - subject1.getSubjectId(); } } |
Output
1 |
TreeSet contains: [Subject -> 103, Subject -> 102, Subject -> 101] |
We can also put a minus sign instead of flipping the arguments to return the descending order as given below.
1 2 3 4 5 6 |
class SubjectComparator implements Comparator<Subject>{ public int compare(Subject subject1, Subject subject2) { return -(subject1.getSubjectId() - subject2.getSubjectId()); } } |
Output
1 |
TreeSet contains: [Subject -> 103, Subject -> 102, Subject -> 101] |
Please also visit how to iterate over TreeSet elements to know more.
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet