android异常 ConcurrentModificationException();

android异常  ConcurrentModificationException();

问题描述:

在对map集合遍历key,并且判断key值是否存在,然后添加对应的value时,put了两次key之后,到第三次再判断时候抛出异常 throw new ConcurrentModificationException();,,

这是主要代码

private Map> modleLists() {
List lists = new ArrayList();
Map> maps = null;
for (int i = 0; i < carInfoLists.size(); i++) {
if (maps == null) {
maps = new HashMap>();
lists.add((carInfoLists.get(i)));
maps.put(carInfoLists.get(i).getModleName(), lists);
} else {
for (String key : maps.keySet()) {
if (key.contains(carInfoLists.get(i).getModleName())) {
CarModle carModle = carInfoLists.get(i);
maps.get(key).add(carModle);
break;
} else {
lists.clear();
lists.add(carInfoLists.get(i));
maps.put(carInfoLists.get(i).getModleName(), lists);
}
}

        }
    }

在迭代maps时,不要对maps再进行操作

迭代到最后,需要removeall