哈喽,大家好,我是了不起。
通常1-3年工作经验的程序员算是初级程序员,再往后基本上就是在编程领域有了一定经验的高级程序员了。
但是最近公司代码review时,我居然发现一个 5 年工作经验的程序员,使用 ArrayList 居然用 forEach 遍历删除元素?
由于公司代码有一定敏感,我这里把代码进行脱敏,大家一起来看看:
public static void main(String[] args) {
ArrayList list = new ArrayList<>(Arrays.asList("1", "2", "3"));
list.forEach(item -> {
if (item.startsWith("1")) {
list.remove(item);
}
});
}
乍看之下,这段代码似乎没什么问题。但实际运行时,它会抛出ConcurrentModificationException异常。
这是为什么呢?我们运行这段代码,报错如下 :
图片
其实 forEach 是一个语法糖,我们编译后的代码如下:
//这是一颗语法糖,编译后相当于:
for(Iterator i = lists.iterator();i.hasNext();){
String s = (String)i.next();
if(s.startsWith("1")){
list.remove(s);
}
}
然后这里的 i.next() 方法:
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];
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
这样就很明了了,在Java中,当我们试图在遍历一个集合的同时修改它时,就会遇到ConcurrentModificationException。这是因为ArrayList的迭代器设计为快速失败(fail-fast),即在检测到集合在迭代期间被修改时立即抛出异常。
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
if (item.startsWith("1")) {
iterator.remove();
}
}
这种方法可以保证在删除元素的同时不会破坏迭代器的状态。
从Java 8开始,ArrayList引入了removeIf方法,这是删除元素的另一种便捷方式:
list.removeIf(item -> item.startsWith("1"));
最后一种方法是首先收集所有需要删除的元素,然后再进行删除:
List itemsToRemove = list.stream()
.filter(item -> item.startsWith("1"))
.collect(Collectors.toList());
list.removeAll(itemsToRemove);
网站题目:五年程序员使用ArrayList居然用forEach遍历删除元素?
当前网址:http://www.shufengxianlan.com/qtweb/news19/329269.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联