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
}
Call
What 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
Parameter
Description
None
descendingIterator() takes no parameters.
Return Value
Return Value
Description
Iterator<E>
An iterator over the elements from tail (last) to head (first).
Works 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);
}
}
Output
code
eat
wake
deque unchanged: [wake, eat, code]
How It Works
Start at the tail. The first next() returns code, the last element.
Move toward the head. Then eat, then wake.
End of the walk. After the head, hasNext() is false.
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);
}
}
Output
[3, 8, 5, 1]
How It Works
Two 8s. They sit in the second and fourth positions.
Unboxed comparison.it.next() == 8 compares an Integer with an int literal, so the Integer is unboxed and the numbers are compared.
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.
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()));
}
}
}