在HashMap中
map = new HashMap<String,String>();
it = map.entrySet().iterator();
while (it.hasNext())
{
entry = it.next();
it.remove(); //safely remove a entry
entry.setValue("new value"); //safely update current value
//how to put new entry set inside this map
//map.put(s1,s2); it throws a concurrent access exception
}
当我尝试向地图中添加新条目时,它会抛出ConcurrentModificationException
异常。对于删除和更新,迭代器具有安全的删除方法。如何在迭代时添加新条目?
Map<String, String> foo
实例并在其中设置所需的值。在您的过程结束时,通过使用map = foo;
将此映射分配给旧映射。 - Luiggi MendozaConcurrentHashMap
来完成,但这似乎有些过度。此外,使用迭代器来执行这些清理任务并不是一个好的选择。 - Luiggi Mendoza