Java集合Iterator迭代器的实现
一、迭代器概述
1、什么是迭代器?
在Java中,有很多的数据容器,对于这些的操作有很多的共性。Java采用了迭代器来为各种容器提供了公共的操作接口。这样使得对容器的遍历操作与其具体的底层实现相隔离,达到解耦的效果。
在Iterator接口中定义了三个方法:
2、迭代器使用
1 2 3 4 5 6 7 8 9 10 11 | public static void main(String[] args) { List<String> list= new ArrayList<>(); list.add( "abc" ); list.add( "edf" ); list.add( "ghi" ); for (Iterator<String> it=list.iterator();it.hasNext();) { System. out .println(it.next()); } } |
执行结果:
二、ArrayList的Iterator实现
1 2 3 4 5 6 7 | private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; ... } |
在ArrayList内部定义了一个内部类Itr,该类实现了Iterator接口。
在Itr中,有三个变量分别是
cursor:表示下一个元素的索引位置
lastRet:表示上一个元素的索引位置
expectModCount:预期被修改的次数
下面看一下Itr类实现了Iterator接口的三个方法:
1 2 3 4 | public boolean hasNext() { return cursor != size; //当cursor不等于size时,表示仍有索引元素 } |
1 2 3 4 5 6 7 8 9 10 11 12 | public E next() //返回下一个元素 { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList. this .elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } |
在next()方法中有一个checkForComodification()方法,其实现为:
1 2 3 4 5 | final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } |
可以看到,该函数是用来判断集合的修改次数是否合法。
在集合内部维护一个字段modCount用于记录集合被修改的次数,每当集合内部结构发生变化(add,remove,set)时,modCount+1。
在迭代器内部也维护一个字段expectedModCount,同样记录当前集合修改的次数,初始化为集合的modCount值。当我们在调用Iterator进行遍历操作时,如果有其他线程修改list会出现modCount!=expectedModCount的情况,就会报并发修改异常java.util.ConcurrentModificationException。下面为示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void main(String[] args) { ArrayList<String> aList= new ArrayList<String>(); aList.add( "bbc" ); aList.add( "abc" ); aList.add( "ysc" ); aList.add( "saa" ); System. out .println( "移除前:" +aList); Iterator<String> it=aList.iterator(); while (it.hasNext()) { if ( "abc" . equals (it.next())) { aList.remove( "abc" ); } } System. out .println( "移除后:" +aList); } |
上面的代码中,如果我们只使用迭代器来进行删除,则不会出现并发修改异常错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void main(String[] args) { ArrayList<String> aList= new ArrayList<String>(); aList.add( "bbc" ); aList.add( "abc" ); aList.add( "ysc" ); aList.add( "saa" ); System. out .println( "移除前:" +aList); Iterator<String> it=aList.iterator(); while (it.hasNext()) { if ( "abc" . equals (it.next())) { it.remove(); } } System. out .println( "移除后:" +aList); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList. this .remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } |
在执行remove操作时,同样先执行checkForComodification(),然后会执行ArrayList的remove()方法,该方法会将modCount值加1,这里我们将expectedModCount=modCount,使之保持统一。
三、ListIterator
上面可以看到,Iterator只提供了删除元素的方法remove,如果我们想要在遍历的时候添加元素怎么办?
ListIterator接口继承了Iterator接口,它允许程序员按照任一方向遍历列表,迭代期间修改列表,并获得迭代器在列表中的当前位置。
ListIterator接口定义了下面几个方法:
下面使用ListIterator来对list进行边遍历边添加元素操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add( "bbc" ); aList.add( "abc" ); aList.add( "ysc" ); aList.add( "saa" ); System. out .println( "移除前:" + aList); ListIterator<String> listIt = aList.listIterator(); while (listIt.hasNext()) { if ( "abc" . equals (listIt.next())) { listIt.add( "haha" ); } } System. out .println( "移除后:" + aList); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人