remove/add注意事项(以ArrayList为例)

本文探讨了在Java开发中,为何建议在foreach循环外使用Iterator进行remove操作,并揭示了并发操作时加锁的重要性,分析了ArrayList内部remove方法的时间复杂性和非原子操作导致的同步需求。
摘要由CSDN通过智能技术生成

目录

 

起因

为什么建议用Iterator方式进行remove操作?

结论

并发操作为什么要加锁?


起因

在看《Java开发手册(泰山版)》发现了这段话:

【强制】不要在 foreach 循环里进行元素的remove/add操作。 remove元素请使用Iterator方式,如果并发操作,需要对Iterator对象加锁。

为什么建议用Iterator方式进行remove操作?

首先查看remove方法:

public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}

public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    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

    return oldValue;
}

再来看看Itr.remove方法:

public void remove() {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();
    try {
        ArrayList.this.remove(lastRet);
        cursor = lastRet;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

结论

①在ArrayList.remove(Object)中,里面有个for循环来从0开始遍历所有元素,来判断其是否与参数obj是否相等,如果采用for-remove方式的话,时间复杂度则是n * n;

②在ArrayList.Itr.remove()中,里面没有for循环,而是直接通过ArrayList.remove(int)方式来移除,时间复杂度比ArrayList.remove(Object)少很多;

并发操作为什么要加锁?

因为ArrayList.Itr.remove()最终调用的还是ArrayList.remove(int),而在ArrayList.remove(int)中出现了modCount++;

这种自增方式,是非原子操作;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值