Java Utility Library

Java ArrayDeque - descendingIterator() Method



The java.util.ArrayDeque.descendingIterator() method returns an iterator over the elements in the deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head) element.

Syntax

public Iterator<E> descendingIterator()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns an iterator over the elements in the deque in reverse sequential order.

Exception

NA

Example:

In the example below, the java.util.ArrayDeque.descendingIterator() method returns an iterator over the elements in the given deque in reverse sequential order. It is further used to display the content of the deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating the ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(40);

    //create a reverse iterator
    Iterator it = MyDeque.descendingIterator();

    //printing the ArrayDeque
    System.out.print("MyDeque contains: "); 
    while(it.hasNext()) 
      System.out.print(it.next()+ " ");  
  }
}

The output of the above code will be:

MyDeque contains: 40 30 20 10 

❮ Java.util - ArrayDeque