I was reading that the best way to iterate over a HashMap to remove entries from it was to use an iterator. I did that. However, I am still getting a ConcurrentModificationException at "i.remove." Does anyone know why?
Iterator<Map.Entry<Integer, ArrayList<JLabel>>> i = tab.getReport().getRows().entrySet().iterator();
while (i.hasNext()) {
Entry e = i.next();
int keyId = (int) e.getKey();
if (toChange.contains(keyId)) {
tab.getReport().getRows().put(keyId - 1, tab.getReport().getRows().get(keyId));
tab.getReport().getRows().get(keyId - 1).get(0).setText(Integer.toString(keyId - 1));
i.remove();
}
}
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.remove(HashMap.java:1443)