Iterator<E> là interface duyệt collection mà không cần biết cấu trúc bên trong (mảng, linked list, tree...).
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if (s.isEmpty()) it.remove(); // xoá AN TOÀN trong khi duyệt
}Lý do quan trọng nhất — xoá an toàn: đoạn sau throw ConcurrentModificationException:
for (String s : list) {
if (s.isEmpty()) list.remove(s); // ❌ CME
}Phải dùng iterator.remove() hoặc list.removeIf(String::isEmpty).
Lưu ý:
- Enhanced for-loop thực ra là syntactic sugar của Iterator.
- ListIterator: chỉ cho List, duyệt 2 chiều + set/add tại chỗ.
- forEach(Consumer) (Java 8+): internal iteration — không remove() trong lúc duyệt, dùng removeIf.
Quy tắc: ưu tiên enhanced for hoặc Stream; dùng iterator tường minh chỉ khi cần remove().
Iterator<E> is the interface for traversing a collection without knowing its internal structure (array, linked list, tree...).
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if (s.isEmpty()) it.remove(); // SAFE removal during iteration
}Most important reason — safe removal: this throws ConcurrentModificationException:
for (String s : list) {
if (s.isEmpty()) list.remove(s); // ❌ CME
}Use iterator.remove() or list.removeIf(String::isEmpty).
Notes:
- Enhanced for-loops are syntactic sugar over Iterator.
- ListIterator: List-only, bi-directional + set/add at position.
- forEach(Consumer) (Java 8+): internal iteration — cannot remove() mid-iteration, use removeIf.
Rule: prefer enhanced for-loops or Streams; reach for an explicit iterator only when you need remove().