java中Collection迭代器的实现
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、迭代器(Iterator)是什么?</li><li>二、Iterator 的核心方法</li><li>三、Iterator 的基本使用步骤(核心)</li><ul class="second_class_ul"><li>完整使用示例</li></ul><li>四、迭代器的关键注意事项(避坑重点)</li><ul class="second_class_ul"></ul><li>五、增强 for 循环(foreach)与迭代器的关系</li><ul class="second_class_ul"></ul><li>总结</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>一、迭代器(Iterator)是什么?</h2><p><code>Iterator</code> 是 Java 集合框架中专门用于<strong>遍历 Collection 集合元素</strong>的接口(位于 <code>java.util</code> 包下),它为所有实现 <code>Collection</code> 接口的集合(如 ArrayList、HashSet 等)提供了<strong>统一的遍历方式</strong>。</p>
<p>你可以把迭代器理解为一个 “集合的专属遍历工具”:它就像一个指针,一开始指向集合第一个元素的 “前面”,通过调用方法可以逐个移动指针、获取元素,而且能安全地在遍历过程中删除元素(这是普通 for 循环做不到的)。</p>
<p class="maodian"></p><h2>二、Iterator 的核心方法</h2>
<p><code>Iterator</code> 接口只有 3 个核心方法,简单且易记:</p>
<table><thead><tr><th>方法</th><th>作用</th></tr></thead><tbody><tr><td>boolean hasNext()</td><td>判断当前指针后面是否还有元素,有则返回 true,无则返回 false</td></tr><tr><td>E next()</td><td>① 将指针向后移动一位 ② 返回当前指针指向的元素</td></tr><tr><td>void remove()</td><td>删除上一次调用 next () 时返回的元素(遍历中安全删除元素的关键)</td></tr></tbody></table>
<p class="maodian"></p><h2>三、Iterator 的基本使用步骤(核心)</h2>
<p>使用迭代器遍历 Collection 集合的固定步骤:</p>
<ol><li>通过 Collection 的 iterator() 方法获取迭代器对象;</li><li>用 hasNext() 判断是否还有下一个元素;</li><li>用 next() 获取下一个元素;</li><li>(可选)用 remove() 删除当前元素。</li></ol>
<p class="maodian"></p><h3>完整使用示例</h3>
<div class="jb51code"><pre class="brush:java;">import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorDemo {
public static void main(String[] args) {
// 1. 创建集合并添加元素
Collection<String> list = new ArrayList<>();
list.add("苹果");
list.add("香蕉");
list.add("橙子");
list.add("葡萄");
// 2. 获取迭代器对象
Iterator<String> it = list.iterator();
// 3. 遍历集合
System.out.println("遍历并输出所有元素:");
while (it.hasNext()) { // 判断是否有下一个元素
String fruit = it.next(); // 移动指针并获取元素
System.out.println(fruit);
// 4. 可选:遍历中删除指定元素(比如删除"香蕉")
if ("香蕉".equals(fruit)) {
it.remove(); // 安全删除,不会触发并发修改异常
}
}
// 遍历结束后查看集合
System.out.println("\n删除香蕉后的集合:" + list); // 输出:[苹果, 橙子, 葡萄]
}
}</pre></div>
<p class="maodian"></p><h2>四、迭代器的关键注意事项(避坑重点)</h2>
<p><strong>调用 next () 前必须先调用 hasNext ()</strong>如果指针已经到集合末尾,直接调用 <code>next()</code> 会抛出 <code>NoSuchElementException</code>(无此元素异常),这是新手最容易犯的错误。</p>
<p><strong>遍历过程中不能用集合的 remove () 方法</strong>如果在迭代器遍历期间,直接调用 <code>collection.remove()</code>(而非 <code>it.remove()</code>),会触发 <code>ConcurrentModificationException</code>(并发修改异常)。因为迭代器会检测集合的 “修改次数”,一旦发现迭代器之外的修改,就会报错。</p>
<div class="jb51code"><pre class="brush:java;">// 错误示例:遍历中直接用集合的 remove 方法
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String fruit = it.next();
if ("香蕉".equals(fruit)) {
list.remove(fruit); // 抛出 ConcurrentModificationException
}
}</pre></div>
<p><strong>remove () 必须在 next () 之后调用</strong>如果先调用 <code>it.remove()</code> 再调用 <code>it.next()</code>,或者连续调用两次 <code>it.remove()</code>,会抛出 <code>IllegalStateException</code>(非法状态异常)。因为 <code>remove()</code> 只能删除 “上一次 next () 获取的元素”,没有 next () 就没有可删除的元素。</p>
<p><strong>迭代器遍历是单向的</strong>迭代器只能从前往后遍历,一旦遍历到末尾,无法回头重新遍历,除非重新获取一个新的迭代器对象。</p>
<p class="maodian"></p><h2>五、增强 for 循环(foreach)与迭代器的关系</h2>
<p>你平时用的增强 for 循环(<code>for (元素类型 变量 : 集合)</code>)本质上是<strong>迭代器的语法糖</strong>,编译器会自动将其编译为迭代器遍历的代码。比如:</p>
<div class="jb51code"><pre class="brush:java;">// 增强 for 循环遍历
for (String fruit : list) {
System.out.println(fruit);
}
// 编译器编译后等价于:
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String fruit = it.next();
System.out.println(fruit);
}</pre></div>
<p>⚠️ 注意:增强 for 循环同样不能在遍历中直接修改集合(比如删除元素),否则也会抛出 <code>ConcurrentModificationException</code>。</p>
<p class="maodian"></p><h2>总结</h2>
<ol><li><code>Iterator</code> 是遍历 <code>Collection</code> 集合的标准工具,提供 <code>hasNext()</code>、<code>next()</code>、<code>remove()</code> 三个核心方法,保证遍历的统一性和安全性。</li><li>迭代器使用的核心规则:先 <code>hasNext()</code> 判断,再 <code>next()</code> 获取,遍历中删除元素必须用 <code>it.remove()</code> 而非集合的 <code>remove()</code>。</li><li>增强 for 循环是迭代器的语法糖,本质相同,但无法手动调用 <code>remove()</code>,遍历中修改集合仍会报错。</li></ol>
<p> 到此这篇关于java中Collection迭代器的实现的文章就介绍到这了,更多相关java Collection迭代器内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Java中不得不知的Collection接口与Iterator迭代器</li><li>Java 数据结构算法Collection接口迭代器示例详解</li><li>Java迭代器与Collection接口超详细讲解</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]