Java Iterator
In this page:
Introduction to Iterator
Iterator provides a uniform way to step through any collection one element at a time via hasNext() and next(), without needing to know whether the underlying collection is a List, Set, or something else.
Example: Introduction to Iterator
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(java.util.List.of("a", "b"));
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Removing Elements with Iterator
Calling iterator.remove() during iteration safely deletes the current element from the underlying collection — this is the only safe way to remove elements while looping, since modifying the collection directly mid-loop throws a ConcurrentModificationException.
Example: Removing Elements with Iterator
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(java.util.List.of(1, 2, 3, 4));
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
if (it.next() % 2 == 0) {
it.remove(); // safe removal during iteration
}
}
System.out.println(list);
}
}
Login to try C/C++/Java/PHP code in the editor
Iterator on Set
A Set has no indexed access, so Iterator is often the only practical way to visit every element in order, since you can't use a traditional indexed for loop the way you could with a List.
Example: Iterator on Set
import java.util.HashSet;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>(java.util.List.of("a", "b"));
Iterator<String> it = set.iterator(); // no indexed access on a Set
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Login to try C/C++/Java/PHP code in the editor
ListIterator (Bidirectional)
ListIterator extends Iterator with the ability to move backward (hasPrevious()/previous()) and to replace or insert elements mid-traversal, capabilities plain Iterator doesn't offer.
Example: ListIterator (Bidirectional)
import java.util.ArrayList;
import java.util.ListIterator;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(java.util.List.of("a", "b"));
ListIterator<String> it = list.listIterator();
while (it.hasNext()) it.next();
while (it.hasPrevious()) {
System.out.println(it.previous()); // moves backward
}
}
}
Login to try C/C++/Java/PHP code in the editor
Safe Modification
Modifying a collection any way other than through the iterator's own methods while iterating (e.g. calling list.remove() directly inside a for-each loop) throws ConcurrentModificationException — always go through the iterator itself for safe mutation.
Example: Safe Modification
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(java.util.List.of(1, 2, 3));
try {
for (Integer n : list) {
list.remove(n); // direct modification during for-each
}
} catch (java.util.ConcurrentModificationException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting