| Author |
Concurrent modification exception in case of remove an item from Collection
|
Ravish Srivastava
Greenhorn
Joined: Sep 05, 2011
Posts: 14
|
|
I have seen the concurrent modification exception normally thrown if a Collection is modified while an Iterator is iterating through it.
I was able to receive concurrent modification exception when i executed below code, as i was iterating the list and was removing the item from list:
But once I commented the code to remove item from list and added a line which add a item in the list, I did not received the exception:
Does concurrent modification exception always comes when we Iterator a collection and removes an item from it? Not in case of addition .
Thanks
RS
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
Hi Ravish,
Welcome to CodeRanch!
Please UseCodeTags(<-click). I've added those for this time.
Here's the thumb rule for modifying the HashMap while iterating it:
When a HashMap is being iterated by an iterator, then any addition/removal of an element must be done via iterator itself.
Currently, you are using an iterator(returned by context.keySet()) to iterate over HashMap, but using methods of HashMap to remove the element from HashMap.
If you look carefully, the ConcurrentModificationException is thrown at line which iterates over HashMap (the for loop in your code), and not at the line which removes an element from HashMap.
Also, in second piece of code, what you are actually doing is: modifying the element Key_2 (and ConcurrentModificationException is thrown only during addition/removal). Try adding a new key/value pair (e.g. context.put("key_3","value_3")) and you should get ConcurrentModificationException.
To fix this, there are two approaches:
1) Use iterator's methods to add/remove from HashMap. e.g.
2) Use ConcurrentHashMap instead of HashMap. In this case, you don't need to follow the iterator rule (that is, your current code will work without throwing ConcurrentModificationException).
I hope this helps.
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
vipul bondugula
Ranch Hand
Joined: Oct 14, 2010
Posts: 218
|
|
|
That's a nice explanation.
|
Thanks
Vipul Kumar
|
 |
 |
|
|
subject: Concurrent modification exception in case of remove an item from Collection
|
|
|