Delete entry from HashMap
This question already has an answer here:
迭代你的地图
Iterator it = adjMap.entrySet().iterator();
while (it.hasNext())
{
Entry item = it.next();
map.remove(item.getKey());
}
I think for-each loops are not allowed to alter the iterated object. To remove entries you should use an iterator.
Compare to map-Iteration for an example.
您可以改为使用ConcurrentHashMap
,或者创建HashMap
的副本并对副本进行任何更改。
上一篇: 如何返回一个HashMap的值
下一篇: 从HashMap中删除条目