0

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)
Pablo
  • 91
  • 2
  • 6
  • 5
    You obviously get that exception, because you edit the map outside of the iterator by using `tab.getReport().getRows().put` ... – Tom May 31 '16 at 11:43
  • 2
    Because you're modifying the map on the 2 lines before `i.remove();`. – Kayaman May 31 '16 at 11:43
  • You can also change `HashMap` to `ConcurrentHashMap` which allows `.remove()` in any kind of loop. – MAZDAK May 31 '16 at 11:48
  • So you can't put element in a HashMap using an iterator? How do you do it then? – Pablo May 31 '16 at 12:04
  • The link provided at the top does not discuss anything about `java.util.Map`, it speaks about `java.util.Collection`. We all know that a `Map` is not a `Collection`. It might be hard for a beginner to understand how to remove safely from `Map` while iterating it from a discussion on 'Collection`. Surely there are other links which do that. But certainly not the link at the top. – Sanjeev Saha May 31 '16 at 12:14
  • Do you have any suggestions to understand how to eliminate items from a Map while iterating? – Pablo May 31 '16 at 12:25
  • @Pablo Sorry on behalf of those who provided a wrong link at the top and closed the issue. I was just about to submit a solution. However you might have a look at: http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap?rq=1 – Sanjeev Saha May 31 '16 at 12:35
  • @SanjeevSaha thank you so much for your help. However, in the link you gave me, he is creating an iterator, then using a while loop and defining a Map.Entry entry variable. How is that different from what I am doing? I am confused on how what I am doing is wrong? – Pablo May 31 '16 at 12:59
  • @Pablo While iterating over `Map.entrySet().iterator()`, you may remove an entry by `Iterator.remove()` safely and also set a new value to an already existing key by `Map.put(key, value)` but you may not `put` a brand new key-value pair. In your code `tab.getReport().getRows().put(keyId - 1, tab.getReport().getRows().get(keyId));` you are doing just that. If you want to iterate and also add new entry to a `Map`, you may use `ConcurrentHashMap`. Thanks for being patient. – Sanjeev Saha May 31 '16 at 15:52

0 Answers0