Java.util.ArrayDeque.descendingIterator() Method


Advertisements


Description

The java.util.ArrayDeque.descendingIterator() method returns an iterator over the elements in this deque in reverse sequential order.

Declaration

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

public Iterator<E> descendingIterator()

Parameters

NA

Return Value

This method returns an iterator over the elements in this deque in reverse sequence.

Exception

NA

Example

The following example shows the usage of java.util.ArrayDeque.descendingIterator() method.

Live Demo
package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(15);
      deque.add(22);
      deque.add(25);
      deque.add(20);

      //iterator() is used to print all the elements
      //next() returns the next element on each iteration
      System.out.println("printing elements using iterator:");
       
      for(Iterator itr = deque.iterator();itr.hasNext();)  {
         System.out.println(itr.next());
      }

      //descendingIterator() is used to print the elements in reverse order
      //next() returns the next element on each iteration
      System.out.println("printing elements in reverse order:");
       
      for(Iterator descItr = deque.descendingIterator();descItr.hasNext();) {
         System.out.println(descItr.next());
      }
   }
 }  

Let us compile and run the above program, this will produce the following result −

printing elements using iterator:
15
22
25
20
printing elements in reverse order:
20
25
22
15

java_util_arraydeque.htm

Advertisements