Java TreeSet - descendingIterator() Method
The java.util.TreeSet.descendingIterator() method returns an iterator over the elements in this set in descending order.
Syntax
public Iterator<E> descendingIterator()
Here, E is the type of element maintained by the container.
Parameters
No parameter is required.
Return Value
Returns an iterator over the elements in this set in descending order.
Exception
NA
Example:
In the example below, the java.util.TreeSet.descendingIterator() method returns an iterator over the elements in the given treeset in descending order. It is further used to display the content of the set.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a treeset TreeSet<Integer> MySet = new TreeSet<Integer>(); //populating the set MySet.add(40); MySet.add(20); MySet.add(10); MySet.add(30); //create a reverse iterator Iterator it = MySet.descendingIterator(); //printing the treeset System.out.print("MySet contains: "); while(it.hasNext()) System.out.print(it.next()+ " "); } }
The output of the above code will be:
MySet contains: 40 30 20 10
❮ Java.util - TreeSet