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

时间:2016-03-12 12:09:18

标签: android concurrentmodification linkedhashmap

我使用迭代器从链接的哈希映射中删除项目但获取并发修改异常

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

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

请帮助..

1 个答案:

答案 0 :(得分:1)

检查条件不是iterator

时,您需要删除linkedMAp
 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