五年程序員使用ArrayList居然用forEach遍歷刪除元素?
哈嘍,大家好,我是了不起。
通常1-3年工作經(jīng)驗(yàn)的程序員算是初級(jí)程序員,再往后基本上就是在編程領(lǐng)域有了一定經(jīng)驗(yàn)的高級(jí)程序員了。
但是最近公司代碼review時(shí),我居然發(fā)現(xiàn)一個(gè) 5 年工作經(jīng)驗(yàn)的程序員,使用 ArrayList 居然用 forEach 遍歷刪除元素?
1、現(xiàn)場(chǎng)還原
由于公司代碼有一定敏感,我這里把代碼進(jìn)行脫敏,大家一起來看看:
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);
}
});
}
乍看之下,這段代碼似乎沒什么問題。但實(shí)際運(yùn)行時(shí),它會(huì)拋出ConcurrentModificationException異常。
這是為什么呢?我們運(yùn)行這段代碼,報(bào)錯(cuò)如下 :
圖片
2、原因分析
其實(shí) forEach 是一個(gè)語法糖,我們編譯后的代碼如下:
//這是一顆語法糖,編譯后相當(dāng)于:
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中,當(dāng)我們?cè)噲D在遍歷一個(gè)集合的同時(shí)修改它時(shí),就會(huì)遇到ConcurrentModificationException。這是因?yàn)锳rrayList的迭代器設(shè)計(jì)為快速失?。╢ail-fast),即在檢測(cè)到集合在迭代期間被修改時(shí)立即拋出異常。
3、如何正確刪除?
3.1 使用迭代器的remove方法
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
if (item.startsWith("1")) {
iterator.remove();
}
}
這種方法可以保證在刪除元素的同時(shí)不會(huì)破壞迭代器的狀態(tài)。
3.2 使用removeIf方法
從Java 8開始,ArrayList引入了removeIf方法,這是刪除元素的另一種便捷方式:
list.removeIf(item -> item.startsWith("1"));
3.3 收集需要?jiǎng)h除的元素
最后一種方法是首先收集所有需要?jiǎng)h除的元素,然后再進(jìn)行刪除:
List<String> itemsToRemove = list.stream()
.filter(item -> item.startsWith("1"))
.collect(Collectors.toList());
list.removeAll(itemsToRemove);