0

Is there any way I can modify the HashMap values of a particular key while iterating over it?

A sample program is given below:

public static void main(String[] args) {
    HashMap<Integer,ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();      
    ArrayList<String> ar = new ArrayList<String>(); 
    for(int i=0;i<50;i++){              
        ar.add(Integer.toString(i));            
    }

    hm.put(1, ar);      

    for(String s:hm.get(1)){
        hm.get(1).add("hello");
    }

}

Error Thrown:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Excp.main(Excp.java:17)
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Raghu
  • 29
  • 2
  • 2
  • 7
  • I don't understand what you are trying to accomplish. The `for` loop just adds `"hello"` to the `ArrayList` for every String in it...couldn't you just add `ArrayList.size()` number of `"hello"` strings to that `ArrayList`? But I have no idea why you would want to do that. – Jared Oct 17 '14 at 05:23

8 Answers8

3

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

Below peice of code is causing the problem.

for(String s:hm.get(1)){
        hm.get(1).add("hello");
    }

You are iterating and modifying the same. Avoid this by creating new ArrayList

  ArrayList<String> ar1 = new ArrayList<String>();

for (String s : hm.get(1)) {
            ar1.add("hello");
        }

have a read here

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 1
    I don't see a point in downvoting this answer, but it did not really mimic the code the OP presented. This solution achieves a quite different goal than what the OP stated (neither of which makes any sense to me). The OP's question seemed to want to append the `ArrayList` at `hm.get(1)` with `"hello"` strings--which this solution does not do. – Jared Oct 17 '14 at 07:56
2

When we are trying to modify collection object while iterating then we get this exception. Check following code:

for(String str : stringList){ 
   stringList.add("Test");
}

So in above we get runtime exception.

Solution

Use iterator over For-Each loop, like:

static void filter(Collection<?> c) {
    for (Iterator<?> it = c.iterator(); it.hasNext(); )
       if (!anyCondition(it.next()))
          it.remove();
}

So basic difference between For-Each and iterator is, we can modify collection while iterating using only iterator.

Mayur Gite
  • 397
  • 4
  • 16
1

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<String> i = ar.listIterator(); i.hasNext(); i.next()) {
    i.add("hello");
} 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

If try to modify while iterating your list you will get this Exception.

for(String s:hm.get(1)){ // iterate 
    hm.get(1).add("hello");//modify
}

Both operation affect to hm

You don't need to iterate here. Just use

hm.get(1).add("hello");
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

If you want to add to the original ArrayList, then iterate through it on your own:

final ArrayList<String> arr = hm.get(1);
final int size = arr.size();

// this will add size number of "hello" strings to ArrayList arr
for(int i = 0; i < size; ++i){
    // you don't appear to ever use this value
    final String s = arr.get(i);

    // do something to arr
    arr.add("hello");
}
Jared
  • 940
  • 5
  • 9
0

Although not related to the question, but just adding

ConcurrentModificationException can also occur if you get the iterator over a collection first and then add some more elements over it and then iterating over the collection will throw this exception.

For example :

package got;

import java.util.*;

public class GotCharacters {


    public static void main(String... args){

        Person p1 = new Person("TL", 40, "Tyrion Lannister");
        Person p2 = new Person("JM", 50, "Jorah Mormont");
        Person p3 = new Person("AS", 20, "Arya Stark");

        //Defining the collection and adding some elements
        ArrayList<Person> al;
        al = new ArrayList<Person>();
        al.add(p1);
        al.add(p2);
        al.add(p3);

        //Getting the iterator
        Iterator<Person> itr = al.iterator();

        Royalty r1 = new Student("DT", 25, "Daenerys Targaryen", "DragonMother", "Targaryen");
        Royalty r2 = new Student("JS", 28, "Jon Snow", "Lord Commander", "Targaryen");
        Collection<Royalty> c = new ArrayList<Royalty>();
        c.add(s1);
        c.add(s2);

        //Adding more elements after getting the iterator
        al.addAll(c);

        while(itr.hasNext()){
            System.out.print(itr.next());
        }
    }
}

Outcome :

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at myArrayList.ArrayList1.main(ArrayList1.java:34)
kunal
  • 81
  • 8
  • Also from the oracle docs : The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. – kunal Aug 15 '17 at 09:52
0

Concurrent Modification in programming means to modify an object concurrently when another task is already running over it. Fail Fast And Fail Safe Iterators in Java Iterators in java are used to iterate over the Collection objects. Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators. Please use ConcurrentHashMap if you want to modify in between.

0

If you can use normal(Iterating over indexes) for loop you can avoid that error.