How to avoid ConcurrentModificationException when iterating over a map and changing values?

前端 未结 7 1009
眼角桃花
眼角桃花 2020-12-06 09:46

I\'ve got a map containing some keys (Strings) and values (POJOs)

I want to iterate through this map and alter some of the data in the POJO.

The current code

相关标签:
7条回答
  • 2020-12-06 10:27

    Two options:

    Option 1

    The current code I've inherited removes the given entry, and adds it back in after making some changes to the POJO.

    Are you changing the reference to the POJO? E.g., so the entry points to something else entirely? Because if not, there's no need to remove it from the map at all, you can just change it.

    Option 2

    If you do need to actually change the reference to the POJO (e.g., the value of the entry), you can still do that in place by iterating over the Map.Entry instances from entrySet(). You can use setValue on the entry, which doesn't modify what you're iterating over.

    Example:

    Map<String,String>                  map;
    Map.Entry<String,String>            entry;
    Iterator<Map.Entry<String,String>>  it;
    
    // Create the map
    map = new HashMap<String,String>();
    map.put("one", "uno");
    map.put("two", "due");
    map.put("three", "tre");
    
    // Iterate through the entries, changing one of them
    it = map.entrySet().iterator();
    while (it.hasNext())
    {
        entry = it.next();
        System.out.println("Visiting " + entry.getKey());
        if (entry.getKey().equals("two"))
        {
            System.out.println("Modifying it");
            entry.setValue("DUE");
        }
    }
    
    // Show the result
    it = map.entrySet().iterator();
    while (it.hasNext())
    {
        entry = it.next();
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
    

    The output (in no particular order) is:

    Visiting two
    Modifying it
    Visiting one
    Visiting three
    two=DUE
    one=uno
    three=tre

    ...without any modification exception. You will probably want to synchronize this in case something else is also looking at / mucking with that entry.

    0 讨论(0)
提交回复
热议问题