In Java, iterator() returns an Iterator that visits the elements of an ArrayDeque from the head to the tail, the same order you see when you print the deque. Call hasNext() to check for another element and next() to get it.
The iterator also supports remove(), which deletes the element most recently returned by next(). The enhanced for loop (for (String s : deque)) uses iterator() behind the scenes. To walk the other way, use descendingIterator().
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 an iterator, then loop while hasNext() is true.
Java
Iterator<String> it = deque.iterator();
while (it.hasNext()) {
String s = it.next();
// use s, or call it.remove()
}
Call
What it does
deque.iterator()
Returns a head-to-tail iterator.
it.hasNext()
true if another element is left.
it.next()
Returns the next element; throws NoSuchElementException past the end.
it.remove()
Removes the element last returned by next().
The iterator is fail-fast: if the deque is changed in any other way while you iterate (for example, with deque.add inside the loop), the next call usually throws ConcurrentModificationException. This check is best effort, so do not rely on it for correctness; just avoid changing the deque except through it.remove().
Library declarations
Java
// java.util.ArrayDeque<E>
public Iterator<E> iterator()
// java.util.Iterator<E>
boolean hasNext()
E next()
void remove()
Parameters
Parameter
Description
None
iterator() takes no parameters.
Return Value
Return Value
Description
Iterator<E>
An iterator over the elements, from head (first) to tail (last).
Compare with Similar Methods
Ways to visit every element:
Method
How it differs
for (E e : deque)
Shorter; uses iterator() internally, but cannot remove elements.
Simpler than it.remove() when you only want to delete matches.
Java Examples
Example 1 — Loop with hasNext() and next()
Build a deque from both ends, then walk it with an explicit iterator.
Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class ArrayDequeIteratorDemo {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>();
d.addLast("b");
d.addFirst("a");
d.addLast("c");
Iterator<String> it = d.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output
a
b
c
How It Works
Head to tail. After the three inserts the deque is [a, b, c], with a at the head.
Get the iterator.d.iterator() starts just before the head.
Step through. Each it.next() returns the next element, so the loop prints a, b, and c.
Stop condition. After c, hasNext() is false and the loop ends without an exception.
Example 2 — Remove Empty Tags with it.remove()
The safe way to delete elements during a loop is through the iterator itself.
Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
public class ArrayDequeIteratorRemove {
public static void main(String[] args) {
Deque<String> tags = new ArrayDeque<>(Arrays.asList("java", "", "deque", "", "code"));
Iterator<String> it = tags.iterator();
int removed = 0;
while (it.hasNext()) {
if (it.next().isEmpty()) {
it.remove();
removed++;
}
}
System.out.println(tags + " removed=" + removed);
}
}
Output
[java, deque, code] removed=2
How It Works
Some blanks. Two of the five tags are empty strings, "".
Test each one.it.next().isEmpty() is true only for the blank tags.
Remove through the iterator.it.remove() deletes the blank just returned, and the iterator keeps its place, so no ConcurrentModificationException occurs.
Clean result. Three tags remain in order and the counter shows 2 removals.
Example 3 — The Enhanced for Loop Uses iterator()
Iterate a stack with a for-each loop, then use an iterator to skip ahead.
Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class ArrayDequeForLoop {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<>();
stack.push("bottom");
stack.push("middle");
stack.push("top");
int position = 1;
for (String s : stack) {
System.out.println(position + ": " + s);
position++;
}
Iterator<String> it = stack.iterator();
it.next();
System.out.println("second from top: " + it.next());
}
}
Output
1: top
2: middle
3: bottom
second from top: middle
How It Works
A pushed stack.top was pushed last, so it is at the head.
for-each order. The loop asks stack.iterator() for elements, so it goes from the head: top, middle, bottom.
Skip one. A fresh iterator's first next() returns top, and the result is ignored.
Read the second. The next next() returns middle, the element just below the top.
Frequently Asked Questions
An Iterator that visits the elements from head (first) to tail (last).
Head to tail, because the loop uses iterator(). For a stack built with push(), that means top first.
Yes, but only with the iterator remove() method. Changing the deque directly inside the loop usually causes ConcurrentModificationException.
Yes. It tries to detect changes made outside the iterator and throws ConcurrentModificationException, but this is best effort, not a guarantee.