Java TreeSet.descendingSet()

Syntax

TreeSet.descendingSet() has the following syntax.

public NavigableSet <E> descendingSet()

Example

In the following code shows how to use TreeSet.descendingSet() method.


/*from  w w w . ja v a  2  s .c  om*/
import java.util.Iterator;
import java.util.TreeSet;

public class Main {
   public static void main(String[] args) {
     
     TreeSet <Integer> treeadd = new TreeSet<Integer> ();
     
     treeadd.add(1);
     treeadd.add(13);
     treeadd.add(17);
     treeadd.add(2);
     
     // creating reverse set
     TreeSet <Integer> treereverse=(TreeSet<Integer>)treeadd.descendingSet();
     
     // create descending set
     Iterator<Integer> iterator = treereverse.iterator();
     
     //Tree set data in reverse order
     while (iterator.hasNext()){
        System.out.println(iterator.next());
     }
   }    
}

The code above generates the following result.