还在 for 循环中 remove 元素?必须劝退……
Posted Java技术栈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了还在 for 循环中 remove 元素?必须劝退……相关的知识,希望对你有一定的参考价值。
来源:juejin.cn/post/6844903906449358856
业务中有需要过滤的需求,踩了 foreach 的坑。本来是这样写的:
user.forEach(u ->
ageList.forEach(a ->
if (u.getId().equals(a))
user.remove(u);
);
);
改进后是这样的:
Iterator<SocNearbyRespDto> ui = user.iterator();
while (ui.hasNext())
SocNearbyRespDto u = ui.next();
ageList.forEach(a->
if (!u.getId().equals(a))
ui.remove();
);
Java 中通常有三种循环
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + ",");
Iterator iterator = list.iterator();
while (iterator.hasNext())
System.out.print(iterator.next() + ",");
for (Integer i : list)
System.out.print(i + ",");
list.foreach
跟代码中第三种增强型 for 循环一样,反编译的内容是
Integer i;
for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i))
i = (Integer)iterator.next();
这样在 for 循环中调用会出现 ConcurrentModificationException
异常。
Iterator是工作在一个独立的线程中,并且拥有一个 mutex 锁。Iterator被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出java.util.ConcurrentModificationException
异常。
所以改成用 Iterator.remove()
就好了。
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
以上是关于还在 for 循环中 remove 元素?必须劝退……的主要内容,如果未能解决你的问题,请参考以下文章
for循环里面不要进行remove操作,for循环里remove元素后,list的下标会减小,导致遍历不完全
普通for循环遍历List时调用remove方法,List没有遍历完。为啥?