java.util.TreeSet.descendingSet() Method


Advertisements


Description

The descendingSet() method is used to return a reverse order view of the elements contained in this set.The descending set is backed by this set, so changes to the set are also reflected in the descending set and reverse.

Declaration

Following is the declaration for java.util.TreeSet.descendingSet() method.

public NavigableSet<E> descendingSet()

Parameters

  • NA

Return Value

The method call returns a reverse order view of this set.

Exception

  • NA

Example

The following example shows the usage of java.util.TreeSet.descendingSet() method.

package com.tutorialspoint;

import java.util.Iterator;
import java.util.TreeSet;

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

Let us compile and run the above program, this will produce the following result.

Tree set data in reverse order: 
17 
13 
2 
1 

java_util_treeset.htm

Advertisements