哈喽,大家好,我是了不起。
通常1-3年工作经验的程序员算是初级程序员,再往后基本上就是在编程领域有了一定经验的高级程序员了。
但是最近公司代码review时,我居然发现一个 5 年工作经验的程序员,使用 ArrayList 居然用 forEach 遍历删除元素?
由于公司代码有一定敏感,我这里把代码进行脱敏,大家一起来看看:
public static void main(String[] args) { ArrayList<String> 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<String> 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<String> itemsToRemove = list.stream() .filter(item -> item.startsWith("1")) .collect(Collectors.toList());list.removeAll(itemsToRemove);
本文链接:http://www.28at.com/showinfo-26-57900-0.html五年程序员使用ArrayList居然用forEach遍历删除元素?
声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。