Java Essentials: Preventing ConcurrentModificationException

本文探讨了在迭代过程中尝试修改集合时Java虚拟机抛出的ConcurrentModificationException,并提供了几种解决方案,包括使用迭代器、创建单独的待删除项列表以及利用Java 8的removeIf()方法。
摘要由CSDN通过智能技术生成

一.参考
1.Java Essentials: Preventing ConcurrentModificationException
https://www.codebyamir.com/blog/java-essentials-preventing-concurrentmodificationexception

二.
Overview
The JVM will throw a ConcurrentModificationException at runtime whenever we try to modify a collection while iterating over it.

In this article, we’ll explain why this happens and some solutions to prevent this.

Example
Suppose we populate a list with names and then later want to remove some names from the list.

Code
We may write something like this:

List names = new ArrayList<>();

names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);

for (String name : names) {
if (name.startsWith(“A”)) {
names.remove(name);
}
}

System.out.println(names);
Output
This code will compile successfully but throw an exception at runtime on line 10:

Exception in thread “main” java.util.ConcurrentModificationException
at java.util.ArrayList I t r . c h e c k F o r C o m o d i f i c a t i o n ( U n k n o w n S o u r c e ) a t j a v a . u t i l . A r r a y L i s t Itr.checkForComodification(Unknown Source) at java.util.ArrayList Itr.checkForComodification(UnknownSource)atjava.util.ArrayListItr.next(Unknown Source)
at com.codebyamir.demo.Main.main(Main.java:10)
Notice that this may only happen intermittently because we are calling remove() inside a conditional statement. So the exception will be thrown whenever we have a String that starts with “A” in our list.

Solutions
There are a number of ways to prevent ConcurrentModificationException, and we’ll explore these below.

Use an Iterator
We can change how we iterate by replacing the enhanced for-loop with a while loop that uses an Iterator object. The Iterator allows us to safely remove the matching element because we are not calling remove() directly on the list object.

Code
List names = new ArrayList<>();

names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);

Iterator iter = names.iterator();

while (iter.hasNext()) {
String name = iter.next();

if (name.startsWith(“A”)) {
iter.remove();
}
}

System.out.println(names);
Output
[Beth, Lucy]

Populate a separate list to keep track of the items to be removed
This approach avoids having to introduce an Iterator object, but it requires another list to keep track of the names we want to remove.

Code
List names = new ArrayList<>();

names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);

List removeNames = new ArrayList<>();

for (String name : names) {
if (name.startsWith(“A”)) {
removeNames.add(name);
}
}

names.removeAll(removeNames);
Output
[Beth, Lucy]

Use Java 8’s removeIf() method
Java 8 added the removeIf() method to the java.util.Collection class.

JDK Code
Let’s take a look at the JDK code for this method. Notice how it uses an Iterator under the hood.

public boolean removeIf(Predicate paramPredicate) {
Objects.requireNonNull(paramPredicate);
boolean bool = false;
Iterator localIterator = iterator();
while (localIterator.hasNext()) {
if (paramPredicate.test(localIterator.next())) {
localIterator.remove();
bool = true;
}
}
return bool;
}
Code
This method makes our code more concise since we can use a Lambda expression.

List names = new ArrayList<>();

names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);

names.removeIf(name -> (name.startsWith(“A”)));
Output
[Beth, Lucy]

Note that ArrayList has an optimized implementation of `removeIf`` (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/ArrayList.java/#1393) which makes it the fastest solution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值