Delete entry from HashMap

This question already has an answer here:

  • Iterate through a HashMap [duplicate] 7 answers

  • 迭代你的地图

    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的副本并对副本进行任何更改。

    链接地址: http://www.djcxy.com/p/17990.html

    上一篇: 如何返回一个HashMap的值

    下一篇: 从HashMap中删除条目