Monday 21 April 2014

NavigableSet : descendingIterator : Get the Descending iterator

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

import java.util.*;

class NavigableSetDescendingIterator{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  
  for(int i=0; i < 15; i++)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  Iterator<Integer> iter = mySet.descendingIterator();
  
  System.out.println("Elements by using Descending iterator");
  while(iter.hasNext()){
   System.out.print(iter.next() +" ");
  }   
 }
}

Output
Elements in mySet are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements by using Descending iterator
14 13 12 11 10 9 8 7 6 5 4 3 2 1 0



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment