Java ArrayDeque descendingIterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, descendingIterator() returns an Iterator that visits the elements of an ArrayDeque in reverse order: from the tail (last) to the head (first). The deque itself is not changed or reordered.

It works just like iterator(): use hasNext() and next(), and remove() deletes the element last returned. It is the easy way to read a deque backwards without copying it.

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.

Syntax

Get the reverse iterator and loop as usual.

Java
Iterator<String> it = deque.descendingIterator();
while (it.hasNext()) {
    System.out.println(it.next());   // last element first
}
CallWhat it does
deque.descendingIterator()Returns a tail-to-head iterator.
it.next()Returns the next element toward the head.
it.remove()Removes the element last returned.
deque.iterator()The normal head-to-tail iterator.

There is no enhanced-for syntax for the reverse direction before Java 21; you write the while loop yourself. Like the normal iterator, it is fail-fast.

Library declarations
Java
// java.util.ArrayDeque<E>
public Iterator<E> descendingIterator()

// Declared in Deque<E>

Parameters

ParameterDescription
NonedescendingIterator() takes no parameters.

Return Value

Return ValueDescription
Iterator<E>An iterator over the elements from tail (last) to head (first).

Compare with Similar Methods

Ways to read a deque backwards:

MethodHow it differs
iterator()The forward direction, head to tail.
pollLast() in a loopAlso tail first, but removes every element.
deque.reversed()Java 21 and later: a reverse-ordered view of the deque, usable in a for-each loop.
toArray() + backward loopWorks on any version, but copies the elements first.

Java Examples

Example 1 — Print a Deque Backwards

Walk the steps from last to first and confirm the deque is unchanged.

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

public class ArrayDequeDescendingDemo {
    public static void main(String[] args) {
        Deque<String> steps = new ArrayDeque<>(Arrays.asList("wake", "eat", "code"));

        Iterator<String> it = steps.descendingIterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("deque unchanged: " + steps);
    }
}

How It Works

  1. Start at the tail. The first next() returns code, the last element.
  2. Move toward the head. Then eat, then wake.
  3. End of the walk. After the head, hasNext() is false.
  4. Only a view. Printing the deque afterwards shows the original head-to-tail order.

Example 2 — Remove the Last 8 with the Reverse Iterator

Search from the tail and remove the first 8 you meet, which is the last 8 in the deque.

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

public class ArrayDequeDescendingRemove {
    public static void main(String[] args) {
        Deque<Integer> nums = new ArrayDeque<>(Arrays.asList(3, 8, 5, 8, 1));

        Iterator<Integer> it = nums.descendingIterator();
        while (it.hasNext()) {
            if (it.next() == 8) {
                it.remove();
                break;
            }
        }
        System.out.println(nums);
    }
}

How It Works

  1. Two 8s. They sit in the second and fourth positions.
  2. Unboxed comparison. it.next() == 8 compares an Integer with an int literal, so the Integer is unboxed and the numbers are compared.
  3. Remove and stop. Walking from the tail, 1 is skipped, the 8 in fourth position matches, it.remove() deletes it, and break ends the loop.
  4. Same as removeLastOccurrence. The result [3, 8, 5, 1] is what removeLastOccurrence(8) would give; the iterator version lets you use any test.

Example 3 — Check for Palindromes

Read each word's characters backwards and compare with the original word.

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

public class ArrayDequePalindrome {
    public static void main(String[] args) {
        String[] words = {"level", "deque"};

        for (String word : words) {
            Deque<Character> chars = new ArrayDeque<>();
            for (char c : word.toCharArray()) {
                chars.addLast(c);
            }

            StringBuilder back = new StringBuilder();
            Iterator<Character> it = chars.descendingIterator();
            while (it.hasNext()) {
                back.append(it.next());
            }
            System.out.println(word + " -> " + back + " palindrome=" + word.equals(back.toString()));
        }
    }
}

How It Works

  1. Characters in order. Each word's letters are added at the tail, so the deque reads the word forwards.
  2. Read in reverse. The descending iterator hands the letters back last to first, and the builder collects them.
  3. level. Backwards it is still level, so equals returns true.
  4. deque. Backwards it becomes euqed, which is different, so the result is false.

Frequently Asked Questions

It returns an Iterator that visits the elements from tail (last) to head (first).
No. It only reads the elements in reverse order; the deque keeps its order.
Yes. Call remove() on the iterator to delete the element that next() just returned.
Since Java 21 you can write for (E e : deque.reversed()). On older versions, use descendingIterator() in a while loop.
Yes, like iterator(). Changing the deque directly during the loop usually throws ConcurrentModificationException.

Next: element()

Read the head without removing it; throws if empty.

element() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful