java.util.TreeSet.descendingIterator() Method

Advertisements


Description

The descendingIterator() method is used to return an iterator over the elements in this set in descending order.

Declaration

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

public Iterator<E> descendingIterator()

Parameters

  • NA

Return Value

The method call returns an iterator over the elements in this set in descending order.

Exception

  • NA

Example

The following example shows the usage of java.util.TreeSet.descendingIterator() 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>();
     
     // adding in the tree set
     treeadd.add(1);
     treeadd.add(13);
     treeadd.add(17);
     treeadd.add(2);
     
     // create descending iterator
     Iterator iterator;
     iterator = treeadd.descendingIterator();
     
     // displaying the Tree set data
     System.out.println("Tree set data in descending 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 descending order: 
17 
13 
2 
1 



Advertisements
Advertisements