Pages

Saturday, June 12, 2010

ConcurrentModificationException

I got this exception today during operating on a List. It seems to me a rare exception. 

I had to remove few items from a list. I was removing one by one with the List.remove(object). Then got this exception. I was looking for a quick solution. Without being bothered about concurrency. I just made another list of elements which has to be removed from my main list. And finally called the List.removeAll(Collection). It worked nice. 

Code is like this.

List myMainList;
iterate over myMainList
if(certain conditions)
elementsToBeReoved.add(theElement);

myMainList.removeAll(elementsToBeRemoved);


No comments:

Post a Comment