Java Iterator
迭代器的使用、遍历中删除与 forEachRemaining。
获取与使用迭代器
collection.iterator() returns one; hasNext() tests for more and next() advances. Calling next() when empty throws NoSuchElementException.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorBasic {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
}
}a b c 遍历中删除
Calling list.remove() inside a for-each throws ConcurrentModificationException because the modification count changes. Use the iterator's own remove(), which updates that count.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorRemove {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Iterator<Integer> it = nums.iterator();
while (it.hasNext()) {
if (it.next() % 2 == 0) {
it.remove();
}
}
System.out.println(nums);
List<Integer> nums2 = new ArrayList<>(List.of(1, 2, 3, 4, 5));
nums2.removeIf(n -> n % 2 == 0);
System.out.println(nums2);
}
}[1, 3, 5]
[1, 3, 5]removeIf is usually cleaner — internally it removes safely via the iterator.forEachRemaining
forEachRemaining(Consumer) hands the remaining elements to a function — concise with lambdas. It consumes them, so hasNext() is false afterwards.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorForEach {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("x", "y"));
Iterator<String> it = list.iterator();
it.next();
it.forEachRemaining(s -> System.out.println("rest: " + s));
System.out.println(it.hasNext());
}
}rest: y
false获取与使用迭代器
collection.iterator() 返回迭代器,hasNext() 判断还有没有元素,next() 取出下一个。next() 在没元素时调用会抛 NoSuchElementException。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorBasic {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
}
}a b c 遍历中删除
用 for-each 遍历时调 list.remove() 会抛 ConcurrentModificationException,因为集合的修改计数变了。正确做法是用迭代器的 remove(),它会同步更新计数。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorRemove {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Iterator<Integer> it = nums.iterator();
while (it.hasNext()) {
if (it.next() % 2 == 0) {
it.remove();
}
}
System.out.println(nums);
List<Integer> nums2 = new ArrayList<>(List.of(1, 2, 3, 4, 5));
nums2.removeIf(n -> n % 2 == 0);
System.out.println(nums2);
}
}[1, 3, 5]
[1, 3, 5]removeIf 更简洁,它内部就是安全地用了迭代器。forEachRemaining
forEachRemaining(Consumer) 把剩余元素一次性交给一个函数处理,配合 lambda 很简洁。它消费掉剩余部分,之后 hasNext() 就为 false。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorForEach {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("x", "y"));
Iterator<String> it = list.iterator();
it.next();
it.forEachRemaining(s -> System.out.println("rest: " + s));
System.out.println(it.hasNext());
}
}rest: y
false