Iterator
An Iterator is an object that traverses a collection one element at a time. Every Collection provides one via the iterator() method. The enhanced for loop (for (var x : coll)) uses an iterator under the hood.
Example
List<String> names = List.of("Ada", "Grace", "Linus");
Iterator<String> it = names.iterator();
while (it.hasNext()) {
String name = it.next();
System.out.println(name);
}
Remove during iteration
You cannot call list.remove(x) during iteration — it throws ConcurrentModificationException. Use iterator.remove() instead:
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
if (it.next() < 0) it.remove(); // safe
}
// Since Java 8, simpler:
numbers.removeIf(n -> n < 0);
ListIterator
ListIterator extends Iterator with bidirectional traversal and in-place mutation (previous(), set(e), add(e)). Only List provides it.
Iterable vs Iterator
Iterable is what a collection is; Iterator is what you get from calling iterator(). Any class implementing Iterable works with the enhanced for loop.