Saturday 19 April 2014

TreeSet : descendingIterator : Get the Descending Iterator

public Iterator<E> descendingIterator()
Returns an iterator over the elements in this set in descending order.

import java.util.*;

class TreeSetDescendingIterator{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to TreeSet */
  for(int i=0; i <10; i++)
 mySet.add(i);
  
  System.out.println("Elements in TreeSet are");
  System.out.println(mySet);
  
   Iterator<Integer> descIter = mySet.descendingIterator();
 
 System.out.println("Elements by descendingIterator");
 while(descIter.hasNext())
  System.out.print(descIter.next() +" ");
 } 
}

Output
Elements in TreeSet are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Elements by descendingIterator
9 8 7 6 5 4 3 2 1 0




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment