Iterating and removing

A for-each loop is the shortest syntax in the collections part, and it is a rewrite of a loop over an Iterator. Understanding what the compiler writes explains three things the syntax hides: why a class of your own can be walked with the same loop, why two iterators over one list are independent, and why a change to the list in the middle of the loop throws.

Iterable and its iterator

Iterable<T> declares one method, iterator(), and Iterator<T> declares hasNext, next and an optional remove. The for-each loop is defined on Iterable, so for (T x : values) means: ask values for an iterator, call hasNext, and then next, until hasNext answers false.

Every collection implements Iterable, and so may any class of yours. The iterator is an object with state, which is why a second call to iterator() starts a second walk from the beginning and the two do not interfere.

A class you can iterate

A countdown is small enough to show the whole contract. The class implements Iterable<Integer> and returns an anonymous Iterator that counts down from the start value:

import java.util.Iterator; public class Countdown implements Iterable<Integer> { private final int start; Countdown(int start) { this.start = start; } @Override public Iterator<Integer> iterator() { return new Iterator<>() { private int current = start; @Override public boolean hasNext() { return current > 0; } @Override public Integer next() { return current--; } }; } public static void main(String[] args) { for (int value : new Countdown(4)) { System.out.print(value + " "); } System.out.println(); int sum = 0; for (int value : new Countdown(4)) { sum += value; } System.out.println(sum); } }

javac -Xlint:all Countdown.java
java Countdown

4 3 2 1
10

The loop runs four times and the second loop sums the same four values, because each Countdown hands out an iterator that starts again. The anonymous class reads start, a field of the outer object, which is the closure of lesson 043; the current variable belongs to the iterator, so it is created afresh for each walk.

Two iterators at once

Two iterators over one list have two positions, and the program reads from them in an interleaved order:

import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Two { public static void main(String[] args) { List<String> words = new ArrayList<>(List.of("a", "b", "c")); Iterator<String> first = words.iterator(); Iterator<String> second = words.iterator(); System.out.println(first.next()); System.out.println(second.next()); System.out.println(second.next()); System.out.println(first.next()); } }

javac -Xlint:all Two.java
java Two

a
a
b
b

The second iterator does not move when the first one does: each answers the element after its own position. That is the property that makes a nested loop over two collections possible, and the reason an iterator is not part of the list.

The fail-fast check

An iterator also holds the modification count it saw when it was created. A structural change through the list updates the count, and the next call on the iterator compares the two and throws:

import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Fail { public static void main(String[] args) { List<String> words = new ArrayList<>(List.of("a", "b", "c")); Iterator<String> it = words.iterator(); System.out.println(it.next()); words.add("d"); try { System.out.println(it.next()); } catch (java.util.ConcurrentModificationException e) { System.out.println("after add: " + e.getClass().getSimpleName()); } System.out.println(words); } }

javac -Xlint:all Fail.java
java Fail

a
after add: ConcurrentModificationException
[a, b, c, d]

The add itself succeeded, as the last line shows; it is the iterator that refuses to continue. The check is called fail-fast because the alternative is worse: an iterator that kept going would read the wrong elements or miss one, and the bug would appear far from its cause.

The exception is thrown by the iterator, not by the list, so the loop that removes must do it through Iterator.remove, which the iterator knows about. Lesson 070 shows the three legal ways to remove while iterating, and removeIf is the shortest.

Walking a list by hand

An iterator that is read by hand can look ahead, which a for-each cannot. The method below groups the elements in pairs, and it asks hasNext a second time before reading the second element of a pair:

import java.util.Iterator; import java.util.List; public class Pairwise { public static void main(String[] args) { System.out.println(pairs(List.of(1, 2, 3, 4))); System.out.println(pairs(List.of(1))); System.out.println(join(List.of("a", "b", "c"), "-")); } static String pairs(List<Integer> numbers) { StringBuilder builder = new StringBuilder(); Iterator<Integer> it = numbers.iterator(); while (it.hasNext()) { int current = it.next(); if (it.hasNext()) { builder.append(current).append("+").append(it.next()).append(" "); } else { builder.append(current); } } return builder.toString().trim(); } static String join(List<String> words, String separator) { StringBuilder builder = new StringBuilder(); for (Iterator<String> it = words.iterator(); it.hasNext(); ) { builder.append(it.next()); if (it.hasNext()) { builder.append(separator); } } return builder.toString(); } }

javac -Xlint:all Pairwise.java
java Pairwise

1+2 3+4
1
a-b-c

The pair method consumes two elements per pass when it can and one when it cannot, which an enhanced for cannot express. The join is the same loop written with Iterator in the header, and the separator goes between the words because hasNext is asked before it is appended. A shorter join is String.join, which lesson 028 shows for the same text.

Exercises

  1. Make Countdown start at 0 and print nothing, and check that both loops behave.
  2. Walk a list backwards with a ListIterator as lesson 070 does and print the elements.
  3. Add an element to a list between the two creations of the two iterators of Two and explain the output.
  4. Write a method that returns every second element of a list using an iterator.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which method does Iterable declare?

    Answer

    iterator()

    hasNext and next belong to the Iterator the method returns.

  2. What does the compiler turn a for-each loop into?

    Answer

    A loop over an Iterator with hasNext and next

    The loop is defined on Iterable, so it asks for an iterator and walks it.

  3. When does an iterator throw ConcurrentModificationException?

    Answer

    When the collection changed structurally since the iterator was made

    The iterator compares its remembered modification count with the collection on each step.

  4. What does this program print? import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Q1 { public static void main(String[] args) { List<String> list = new ArrayList<>(List.of("a", "b")); Iterator<String> it = list.iterator(); System.out.println(it.next()); System.out.println(it.hasNext()); System.out.println(it.next()); System.out.println(it.hasNext()); } }

    Answer

    a / true / b / false

    The first element leaves one behind, and after the second none remains.

  5. What does this program print? import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Q2 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4)); Iterator<Integer> it = list.iterator(); int sum = 0; while (it.hasNext()) { sum += it.next(); } System.out.println(sum); System.out.println(it.hasNext()); } }

    Answer

    10 / false

    The loop adds all four and leaves the iterator at its end.

  6. What does this program print? import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Q3 { public static void main(String[] args) { List<String> list = new ArrayList<>(List.of("a", "b", "c")); Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); if (value.equals("b")) { it.remove(); } } System.out.println(list); } }

    Answer

    [a, c]

    The iterator removes through its own method, so the count stays consistent.

  7. What does this program print? import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Q4 { public static void main(String[] args) { List<String> list = new ArrayList<>(List.of("a")); Iterator<String> it = list.iterator(); list.add("b"); try { it.next(); } catch (java.util.ConcurrentModificationException e) { System.out.println("changed"); } } }

    Answer

    changed

    The add changed the list after the iterator was created, so the next call throws.

  8. What does this program print? import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Q5 { public static void main(String[] args) { List<String> list = new ArrayList<>(List.of("a", "b", "c")); Iterator<String> a = list.iterator(); Iterator<String> b = list.iterator(); a.next(); b.next(); b.next(); System.out.println(a.next() + b.next()); } }

    Answer

    bc

    The two iterators keep separate positions, so a is at b and b is at c.

  9. What must a class implement for a for-each loop to accept it?

    Answer

    Iterable, which declares iterator()

    The loop asks the object for an iterator, and Iterable is the interface that promises one.

  10. Why is the fail-fast check better than an iterator that continues?

    Answer

    It reports the mistake where it happens instead of reading the wrong elements

    A walk over changed data would give a wrong result far from the cause, so the iterator refuses.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev