我们都知道你不能这样做:
for (Object i : l) {
    if (condition(i)) {
        l.remove(i);
    }
} 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);
}当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException... 即使多线程没有这样做...... 无论如何。
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在这里使用任意Collection ,不一定是ArrayList ,所以你不能依赖get 。 
 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();
    }
}请注意, Iterator.remove()是在迭代期间修改集合的唯一安全方法; 如果在迭代进行过程中以任何其他方式修改基础集合,则行为未指定。 
同样,如果你有一个ListIterator并且想要添加项目,你可以使用ListIterator#add ,原因与你可以使用Iterator#remove相同 - 它的设计允许它。 
在您的情况下,您尝试从列表中删除,但如果在迭代其内容时尝试put Map ,则会应用相同的限制。 
这有效:
Iterator<Integer> iter = l.iterator();
while (iter.hasNext()) {
    if (iter.next() == 5) {
        iter.remove();
    }
}我假设因为 foreach 循环是用于迭代的语法糖,使用迭代器无济于事...... 但它为您提供了这个.remove()功能。 
使用 Java 8,您可以使用新的removeIf方法 。适用于您的示例: 
Collection<Integer> coll = new ArrayList<>();
//populate
coll.removeIf(i -> i == 5);