跳到主要内容
  1. 所有文章/
  2. Java基础知识笔记汇总/

不要在foreach中进行元素的remove!

·📄 1611 字·🍵 4 分钟

原理探究 #

先看一下这个代码,运行时会报错:java.util.ConcurrentModificationException。

public static void main(String[] args) {
    List<String> integerList = new ArrayList<String>() {{
        add("1");
        add("2");
    }};
    for (String ele : integerList) {
        if ("2".equals(ele))
            integerList.remove(ele);
    }
}

探究一下(java.util.ConcurrentModificationException)的形成:

首先源码会被编译成这个,可以发现是用了Iterator 来遍历元素,但是删除元素是用的列表自带的删除方法。

public static void main(String[] args) {
    List<String> integerList = new ArrayList<String>() {{
        add("1");
        add("2");
    }};
    Iterator<String> iterator = integerList.iterator();
    while (iterator.hasNext()) {
        String ele = iterator.next();
        System.out.println(ele);
        if ("2".equals(ele))
            integerList.remove(ele);
    }
}

探究一下Iterator.next()方法可以发现一开始会调用checkForComodification方法,正是这个方法导致了java.util.ConcurrentModificationException。它的一个关键判断就是expectedModCount != ArrayList.this.modCount

image-20240307120600749.png

image-20240307120718466.png

为什么这两个值不一致就需要报错?

modCount:修改次数,对集合做remove,and等操作会触发修改次数(modCount)的增加。

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
}

expectedModCount:期望修改次数

java.util.ConcurrentModificationException:并发修改异常

原因:java.util包下的集合类都是快速失败的,不能在多线程下并发修改或者迭代过程中被修改。比如ArrayList和LinkedList。

快速失败(fail-fast) #

快速失败:迭代器在遍历时直接访问集合中的内容,并且在遍历过程中使用一个 modCount 变量。集合在被遍历期间如果内容发生变化,就会改变modCount的值。每当迭代器使用hashNext()/next()遍历下一个元素之前,都会检测modCount变量是否为expectedmodCount值,是的话就返回遍历;否则抛出异常,终止遍历。

这里异常的抛出条件是检测到 modCount!=expectedmodCount 这个条件。如果集合发生变化时修改modCount值刚好又设置为了expectedmodCount值,则异常不会抛出。因此,不能依赖于这个异常是否抛出而进行并发操作的编程,这个异常只建议用于检测并发修改的bug

安全失败(fail-safe) #

采用安全失败机制的集合容器,在遍历时不是直接在集合内容上访问的,而是先复制原有集合内容,在拷贝的集合上进行遍历。由于迭代时是对原集合的拷贝进行遍历,所以在遍历过程中对原集合所作的修改并不能被迭代器检测到,所以不会触发ConcurrentModificationException。

缺点:基于拷贝内容的优点是避免了Concurrent Modification Exception,但同样地,迭代器并不能访问到修改后的内容,即:迭代器遍历的是开始遍历那一刻拿到的集合拷贝,在遍历期间原集合发生的修改迭代器是不知道的。

场景:java.util.concurrent包下的容器都是安全失败,可以在多线程下并发使用,并发修改。比如:CopyOnWriteArrayList。

最终结论 #

如果需要在迭代中遍历的话,可以使用迭代器的删除方法:

public static void main(String[] args) {
    List<String> integerList = new ArrayList<String>() {{
        add("1");
        add("2");
    }};
    Iterator<String> iterator = integerList.iterator();
    while (iterator.hasNext()) {
        String test = iterator.next();
        System.out.println(test);
        if (Objects.equals(test, "2"))
            iterator.remove();
    }
}

iterator.remove()方法会重置expectedModCount=modCount:

image-20240307122135581.png

特殊情况 #

可以发现只要调用list的删除方法,刚好删除list的倒数第二个元素,刚好不会报错: 这是因为删除list的倒数第二个元素时,刚好改变了数组的大小,导致了迭代器提前结束了遍历,上诉代码运行后可以发现:它只打印了第一个元素就遍历结束了。生产中使用的话会导致各种奇怪的结果!

public static void main(String[] args) {
    List<String> integerList = new ArrayList<String>() {{
        add("1");
        add("2");
    }};
    Iterator<String> iterator = integerList.iterator();
    while (iterator.hasNext()) {
        String test = iterator.next();
        System.out.println(test);
        if (Objects.equals(test, "1"))
            integerList.remove(test);
    }
}

修改为iterator.remove();之后,则不会有这个问题。

public static void main(String[] args) {
    List<String> integerList = new ArrayList<String>() {{
        add("1");
        add("2");
    }};
    Iterator<String> iterator = integerList.iterator();
    while (iterator.hasNext()) {
        String test = iterator.next();
        System.out.println(test);
        if (Objects.equals(test, "1"))
            iterator.remove();
    }
}