并发修改异常链接哈希映射 android

concurrent modification exception linked hash map android

我正在使用迭代器从链接的哈希映射中删除项目,但出现并发修改异常

Iterator<Integer> it = linkedMap.keySet().iterator();

    while (it.hasNext()) {
        java.lang.Integer key = it.next();
        if (key.equals(number)) {
            linkedMap.remove(key);
        }
    }

请帮忙..

当检查条件而不是 linkedMAp

时,您需要删除 iterator
 Iterator<Integer> it = linkedMap.keySet().iterator();
    while (it.hasNext()) {
     Integer key = it.next();
        if (key.equals(number)) {
           // Remove the current element from the iterator and the list.
            it.remove();
        }
    }

检查一下这个问题是否和你的一样

Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop