The problem in the code your presented isn't modifying the HashMap
, it's modifying the ArrayList
while iterating it.
You can avoid this exception if you use ar
's ListIterator
instead of using an enhanced for
loop:
for (ListIterator i = ar.listIterator(); i.hasNext(); i.next()) {
i.add("hello");
}