java集合遍历删除指定元素异常分析总结
在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又遇到了这个问题,因此,总结下遍历集合的同时如何删除集合中指定的元素;
1.错误场景复原
public class ListRemoveTest {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User("liu1",24));
users.add(new User("liu2",24));
users.add(new User("liu3",24));
users.add(new User("liu4",24));
Iterator<User> iterator = users.iterator();
while(iterator.hasNext()) {
User user = iterator.next();
if(user.getName().equals("liu2")) {
users.remove(user);
}
System.out.println(user);
}
}
}
或者如下代码
public class ListRemoveTest {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User("liu1",24));
users.add(new User("liu2",24));
users.add(new User("liu3",24));
users.add(new User("liu4",24));
for (User user : users) {
if(user.getName().equals("liu2")) {
users.remove(user);
}
System.out.println(user);
}
}
}
以上两种用法都会跑出如下异常:

2.原因分析
上面两种错误,我想很多人都遇到过,这是我们很容易犯的错误,但是为啥会出现上述异常呢,我们又该如何正确遍历集合的同时,删除指定的元素呢!
2.1 原因解析
首先,对于foreach循环遍历,本质上还是迭代器的模式,上面的for语句等价于如下代码:
for (Iterator<User> iterator = users.iterator(); iterator.hasNext();) {
User user = iterator.next();
if(user.getName().equals("liu2")) {
users.remove(user);
}
System.out.println(user);
}
因此,上述错误的本质,就要看迭代器iterator的源码啦
在ArrayList中,它的修改操作(add/remove)都会对modCount这个字段+1,modCount可以看作一个版本号,每次集合中的元素被修改后,都会+1(即使溢出)。
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
接下来再看看AbsrtactList中iteraor方法
public Iterator<E> iterator() {
return new Itr();
}
它返回一个内部类,这个类实现了iterator接口,代码如下:
private class Itr implements Iterator<E> {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
// 修改expectedModCount 的值
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
在内部类Itr中,有一个字段expectedModCount ,初始化时等于modCount,即当我们调用list.iterator()返回迭代器时,该字段被初始化为等于modCount。在类Itr中next/remove方法都有调用checkForComodification()方法,在该方法中检测modCount == expectedModCount,如果不相等则抛出ConcurrentModificationException。
前面说过,在集合的修改操作(add/remove)中,都对modCount进行了+1。
在迭代过程中,执行list.remove(val),使得modCount+1,当下一次循环时,执行
it.next(),checkForComodification方法发现modCount != expectedModCount,则抛出异常。
2.2 预期结果不对,但是不抛异常
注意:还有一种更坑的场景,当删除集合的倒数第二个元素时,程序不会抛出任何异常,只是结果与预期的不相符,如果在应用过程中不认真观察,很难发现该错误!
错误实例如下:
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User("liu1",24));
users.add(new User("liu2",24));
users.add(new User("liu3",24));
users.add(new User("liu4",24));
Iterator<User> iterator = users.iterator();
while(iterator.hasNext()) {
User user = iterator.next();
if(user.getName().equals("liu3")) {
users.remove(user);
}
System.out.println(user);
}
}
运行结果如下:

遍历过程删除了倒数第二个元素,那么最后一个元素就永远遍历不到了,这个主要原因就是Iterator源码中hasNext方法中,判断当前元素下标和集合大小是否相等
public boolean hasNext() {
return cursor != size;
}
当删除倒数第二个元素后,当前元素下标和集合的大小相等了,跳出了循环,就会遍历最后一个集合元素了;
3.正确用法
要想在集合遍历的过程中删除指定元素,就务必使用迭代器自身的remove方法;
再来看看内部类Itr的remove()方法,在删除元素后,有这么一句expectedModCount
= modCount,同步修改expectedModCount
的值。所以,如果需要在使用迭代器迭代时,删除元素,可以使用迭代器提供的remove方法。
其他集合(Map/Set)使用迭代器迭代也是一样。
所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。
但你可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致
具体正确用法代码如下:
public class ListRemoveTest {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
users.add(new User("liu1",24));
users.add(new User("liu2",24));
users.add(new User("liu3",24));
users.add(new User("liu4",24));
Iterator<User> iterator = users.iterator();
while(iterator.hasNext()) {
User user = iterator.next();
if(user.getName().equals("liu2")) {
iterator.remove();
}
System.out.println(user);
}
System.out.println(users);
}
}
运行结果如下:

与预期结果一致;
java集合遍历删除指定元素异常分析总结的更多相关文章
- vector list map 遍历删除指定元素
#include <stdio.h> #include <stdint.h> #include <vector> #include <list> #in ...
- Java集合——遍历集合元素并修改
Java集合——遍历集合元素并修改 摘要:本文主要总结了遍历集合的方式,以及在遍历时修改集合要注意的问题. 遍历Collection 对List和Set的遍历,有四种方式,下面以ArrayList为例 ...
- java集合系列之ArrayList源码分析
java集合系列之ArrayList源码分析(基于jdk1.8) ArrayList简介 ArrayList时List接口的一个非常重要的实现子类,它的底层是通过动态数组实现的,因此它具备查询速度快, ...
- Java集合系列[4]----LinkedHashMap源码分析
这篇文章我们开始分析LinkedHashMap的源码,LinkedHashMap继承了HashMap,也就是说LinkedHashMap是在HashMap的基础上扩展而来的,因此在看LinkedHas ...
- java集合系列之LinkedList源码分析
java集合系列之LinkedList源码分析 LinkedList数据结构简介 LinkedList底层是通过双端双向链表实现的,其基本数据结构如下,每一个节点类为Node对象,每个Node节点包含 ...
- jquery数组删除指定元素的方法:grep()
jquery数组删除指定元素的方法:grep() 金刚 数组 jquery javascript 元素 遇到的问题 今天遇到一个问题,删除数组中的一个指定元素,并返回新的数组. 我定义的js数组是这样 ...
- java集合循环删除
java集合循环删除,java list集合操作,java循环.分享牛,分享牛原创.java集合删除方法. 2.6.1.第一种方式 list.add("1"); list.add( ...
- List 循环删除 指定元素的 方法
使用Iterator进行循环,在删除指定元素.如果使用for 或 foreach 在删除指定下标是,list.size 会相应的缩短且下标前移,导致相邻满足条件的元素未删除 Iterator<S ...
- ES6数组中删除指定元素
知识点: ES6从数组中删除指定元素 findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. arr.splice(arr.findIndex(item => ...
随机推荐
- 如何在 js 代码中使用 jsp 标签或 Java 代码
JSP 标签还是很方便的,比如 Struts.Spring 等提供给我们的 JSP 标签,可以用它们来获取变量或进行一些计算.比如 struts2 的 <s:url value="/a ...
- 为什么写《Tomcat内核设计剖析》
三四年前更多地还是做web业务开发,基本不关心web层以下的东西,但是每次出故障时面对现象都不能从脑子里形成由底层到应用层的完整的逻辑,往往只能分析到Web应用就无法继续往下,Web容器完全就是一个黑 ...
- python sys.path.append()和sys.path.insert()
python程序中使用 import XXX 时,python解析器会在当前目录.已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错. 使用sys.path.append()方法可以临时添加搜索 ...
- python 怎么画图
1 安装matplotlib: 安装方法:http://www.2cto.com/os/201309/246928.html(其中,安装过程中,tar解压怎么解都有问题.然后就删掉再下载一遍) 2 使 ...
- Python源码分析(一)
最近想学习下Python的源码,希望写个系列博客,记录的同时督促自己学习. Python源码目录 从Python.org中下载源代码压缩包并解压,我下载的是Python2.7.12,解压后: 对于主要 ...
- Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
- 最新博客开启 - Noogle's Blogs
博客地址: http://noogel.xyz/ 戳我进入 Noogle's Blogs
- 线性回归 Linear regression(2)线性回归梯度下降中学习率的讨论
这篇博客针对的AndrewNg在公开课中未讲到的,线性回归梯度下降的学习率进行讨论,并且结合例子讨论梯度下降初值的问题. 线性回归梯度下降中的学习率 上一篇博客中我们推导了线性回归,并且用梯度下降来求 ...
- WebStorm的下载与安装
百度搜索: 链接:http://www.jetbrains.com/webstorm/ 链接:http://www.jetbrains.com/student/ 学生免费授权计划 请从正规来源下载软件 ...
- .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
本文将解释在 .NET 技术栈中各种不同使用方式下 N E T 三个字母何时大写何时小写:前面的 “.” 什么时候加上,什么时候去掉,什么时候又使用 “dot”. .NET 在技术文档中 如果你阅 ...