w3resource

Java ArrayDeque Class: descendingIterator() Method

public Iterator<E> descendingIterator()

The descendingIterator() method is used to iterator over the elements in a given deque in reverse sequential order. The elements return in order from last (tail) to first (head).

Package: java.util

Java Platform: Java SE 8

Syntax:

descendingIterator()

Return Value:

An iterator over the elements in this deque in reverse sequence

Return Value Type: Iterator<E - the type of elements held in this collection>

Pictorial Presentation

Java ArrayDeque Class: descendingIterator() Method

Example: Java ArrayDeque Class: descendingIterator() Method

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

public class Main {
   public static void main(String[] args) {
   Deque<Integer> deque = new ArrayDeque<Integer>(8);
   
    // use add() method to add elements in the deque
      deque.add(10);
      deque.add(20);
      deque.add(30);
      deque.add(40);

      
      System.out.println("Print all the elements of the original deque:");
       
      for(Iterator itr = deque.iterator(); itr.hasNext();)  {
         System.out.println(itr.next());
      }

       System.out.println("Print all the elements of the original deque in reverse order:");
       
      for(Iterator desc_itrr = deque.descendingIterator();desc_itrr.hasNext();) {
         System.out.println(desc_itrr.next());
      }
   }
 }
 

Output:

Print all the elements of the original deque:
10
20
30
40
Print all the elements of the original deque in reverse order:
40
30
20
10

Java Code Editor:

Previous: contains Method
Next: element Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_descendingIterator.php