Saturday 12 April 2014

descendingIterator : Iterate reverse order

Iterator<E> descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

class DequeReverseIterate{
  public static void main(String args[]){
    Deque<Integer> myDeque = new LinkedBlockingDeque<Integer> (5);
  
  myDeque.add(10);
  myDeque.add(20);
  myDeque.add(30);
  myDeque.add(40);
  myDeque.add(10);
  
  System.out.println("Elements in Deque are " + myDeque);
  
  Iterator<Integer> iter = myDeque.descendingIterator();
  System.out.println("Elements in Reverse Order are");
  while(iter.hasNext()){
  System.out.print(iter.next()+" ");
  }
  }
}

Output
Elements in Deque are [10, 20, 30, 40, 10]
Elements in Reverse Order are
10 40 30 20 10

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment