Cla*_*diu 1158 java iteration collections
我们都知道你不能这样做:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException
等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
...即使多线程没有这样做......无论如何.
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在Collection
这里使用任意,不一定是ArrayList
,所以你不能依赖get
.
Bil*_*l K 1568
Iterator.remove()
是安全的,你可以像这样使用它:
List<String> list = new ArrayList<>();
// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
// Iterator<String> iterator = list.iterator();
// while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string.isEmpty()) {
// Remove the current element from the iterator and the list.
iterator.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这Iterator.remove()
是在迭代期间修改集合的唯一安全方法; 如果在迭代进行过程中以任何其他方式修改基础集合,则行为未指定.
同样,如果你有一个ListIterator
并且想要添加项目,你可以使用ListIterator#add
,出于同样的原因你可以使用Iterator#remove
它 - 它的设计允许它.
你的情况,你想从列表中删除,但同样的限制,如果想put
成为一个Map
在迭代的内容.
Cla*_*diu 339
这有效:
Iterator<Integer> iter = l.iterator();
while (iter.hasNext()) {
if (iter.next() == 5) {
iter.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
我假设因为foreach循环是用于迭代的语法糖,使用迭代器无济于事......但它给你这个.remove()
功能.
ass*_*ias 197
使用Java 8,您可以使用新removeIf
方法.适用于您的示例:
Collection<Integer> coll = new ArrayList<>();
//populate
coll.removeIf(i -> i == 5);
Run Code Online (Sandbox Code Playgroud)
Ash*_*ish 41
由于问题已经得到解答,即最好的方法是使用迭代器对象的remove方法,我将进入"java.util.ConcurrentModificationException"
抛出错误的地方的细节.
每个集合类都有它实现了Iterator接口,并提供了类似方法的私有类next()
,remove()
和hasNext()
.
next的代码看起来像这样......
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
Run Code Online (Sandbox Code Playgroud)
这里的方法checkForComodification
实现为
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
Run Code Online (Sandbox Code Playgroud)
因此,正如您所看到的,如果您明确尝试从集合中删除元素.它导致modCount
与众不同expectedModCount
,导致异常ConcurrentModificationException
.
Rod*_*own 26
你可以像你提到的那样直接使用迭代器,或者保留第二个集合并将要删除的每个项目添加到新集合中,然后在最后删除所有项目.这允许你继续使用for-each循环的类型安全性,代价是增加内存使用和cpu时间(除非你有真正的大型列表或真正的旧计算机,否则不应该是一个大问题)
public static void main(String[] args)
{
Collection<Integer> l = new ArrayList<Integer>();
Collection<Integer> itemsToRemove = new ArrayList<Integer>();
for (int i=0; i < 10; ++i) {
l.add(new Integer(4));
l.add(new Integer(5));
l.add(new Integer(6));
}
for (Integer i : l)
{
if (i.intValue() == 5)
itemsToRemove.add(i);
}
l.removeAll(itemsToRemove);
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
Lan*_*dei 17
在这种情况下,一个常见的伎俩(是?)倒退:
for(int i = l.size() - 1; i >= 0; i --) {
if (l.get(i) == 5) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
尽管如此,我很乐意,你必须在Java中8,如更好的方式removeIf
或filter
在流.
Ant*_*tzi 16
与Claudius相同的答案有一个for循环:
for (Iterator<Object> it = objects.iterator(); it.hasNext();) {
Object object = it.next();
if (test) {
it.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
Don*_*aab 11
使用Eclipse Collections(以前称为GS Collections),MutableCollection上removeIf
定义的方法将起作用:
MutableList<Integer> list = Lists.mutable.of(1, 2, 3, 4, 5);
list.removeIf(Predicates.lessThan(3));
Assert.assertEquals(Lists.mutable.of(3, 4, 5), list);
Run Code Online (Sandbox Code Playgroud)
使用Java 8 Lambda语法,可以编写如下:
MutableList<Integer> list = Lists.mutable.of(1, 2, 3, 4, 5);
list.removeIf(Predicates.cast(integer -> integer < 3));
Assert.assertEquals(Lists.mutable.of(3, 4, 5), list);
Run Code Online (Sandbox Code Playgroud)
Predicates.cast()
这里需要调用,因为在Java 8中removeIf
的java.util.Collection
接口上添加了一个默认方法.
注意:我是Eclipse Collections的提交者.
Pri*_*shi 10
制作现有列表的副本并迭代新副本.
for (String str : new ArrayList<String>(listOfStr))
{
listOfStr.remove(/* object reference or index */);
}
Run Code Online (Sandbox Code Playgroud)
带有传统的for循环
ArrayList<String> myArray = new ArrayList<>();
for (int i = 0; i < myArray.size(); ) {
String text = myArray.get(i);
if (someCondition(text))
myArray.remove(i);
else
i++;
}
Run Code Online (Sandbox Code Playgroud)
人们断言一个人无法从被foreach循环迭代的Collection中删除.我只是想指出技术上是不正确的并准确描述(我知道OP的问题是如此先进以至于不知道这一点)这个假设背后的代码:
for (TouchableObj obj : untouchedSet) { // <--- This is where ConcurrentModificationException strikes
if (obj.isTouched()) {
untouchedSet.remove(obj);
touchedSt.add(obj);
break; // this is key to avoiding returning to the foreach
}
}
Run Code Online (Sandbox Code Playgroud)
并不是你不能从迭代中删除Colletion
而是你不能再继续迭代.因此break
在上面的代码中.
抱歉,如果这个答案是一个有点专业的用例,并且更适合我从这里来到这里的原始帖子,那个被标记为重复(尽管这个线程看起来更细致)并锁定.
ConcurrentHashMap或ConcurrentLinkedQueue或ConcurrentSkipListMap可能是另一种选择,因为它们永远不会抛出任何 ConcurrentModificationException,即使您删除或添加项目也是如此。