无法运行hashmap的循环方法

时间:2013-07-21 11:00:23

标签: java hashmap

private void checkMaxPopularity() throws Exception {
    for (StoredArticle ckeckMax : monitorCombination.keySet()) {
        if (ckeckMax.getTimeAndPopularity().containsValue(5)) {
            monitorCombination.remove(ckeckMax);
            ReadXml newReadXml = new ReadXml();
            StoredArticle replaceArticle = getReplaceArticle();
            monitorCombination.put(replaceArticle, newReadXml);
            // System.out.print("TestTestTesttesttest");
        }
    }
}

请帮助我是java的新手,我想迭代一个hashmap以删除一对键和值。在for循环中有一个if语句来检查哪个值达到5然后它将被删除。但我得到了这个例外:

java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at uk.ac.ncl.fanyaoxia.monitor.MonitorRecentUpdates.checkMaxPopularity(MonitorRecentUpdates.java:138)
at uk.ac.ncl.fanyaoxia.monitor.MonitorRecentUpdates.continueMonitor(MonitorRecentUpdates.java:49)
at uk.ac.ncl.fanyaoxia.gui.MonitorRecentUpdatesGUI.actionPerformed(MonitorRecentUpdatesGUI.java:102)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6382)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:683)
at java.awt.EventQueue$3.run(EventQueue.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1 个答案:

答案 0 :(得分:1)

问题是您正在使用来自monitorCombination.keySet()的迭代器,如果您在迭代时修改了地图的内容(添加或删除键),则会抛出该异常。试试这个:

private void checkMaxPopularity() throws Exception {
    for (StoredArticle ckeckMax : new HashSet<StoredArticle>(monitorCombination.keySet())) {
        if (ckeckMax.getTimeAndPopularity().containsValue(5)) {
            monitorCombination.remove(ckeckMax);
            ReadXml newReadXml = new ReadXml();
            StoredArticle replaceArticle = getReplaceArticle();
            monitorCombination.put(replaceArticle, newReadXml);
            // System.out.print("TestTestTesttesttest");
        }
    }
}

(我改变的部分是在for子句中)