4

I am working on an application which has some legacy code. Here, there is a linkedlist and the code iterates that linklist using an iterator in a while loop.

 LinkedList ll = grammarSection.getSectionsAsLinkList();
 Iterator iter = ll.iterator();
 int i=0;
 while (iter.hasNext()) {
 1. GrammarSection agrammarSection = (GrammarSection) iter.next();
 2. grammarLineWithMatches = m_grammarLineMatcher.getMatch(agrammarSection, p_line);
 3. if (grammarLineWithMatches != null) { //condition a 
 4. if (getPeek(ll)!=agrammarSection)
 5. ll.addFirst(ll.remove(i)); //changing the linkedlist Line5
 return grammarLineWithMatches;
 }
 i++;
 }

In the while loop, if condition a is true, then the linkedlist is modified as in line5. However, in this case, the next method on line1 throws a ConcurrentModificationException. How to add and delete the linkedlist without getting any ConcurrentModificationException

asked May 16, 2011 at 13:40
2

3 Answers 3

3

You can't change the collection you are currently iterating. You can:

  • create a copy of it and iterate the copy instead
  • don't use iterator - loop from 0 to list.size(). But with LinkedList this is not efficient.

If it was only about removal, you can use iter.remove(), but you also have addFirst(..)

answered May 16, 2011 at 13:42
1

The short answer is: you can't.

The JDK's List implementations are designed to be modified by the iterator, to preserve order of iteration (there's no way to tell whether an arbitrary list change will do this, so the iterator assumes the worst).

The solution, in your case, is to create a new LinkedList. As you iterate through, either add the iterated elements to the end or beginning of the new list. Then throw away the old.

answered May 16, 2011 at 13:44
4
  • ..NOT to be modified by the iterator? Commented May 16, 2011 at 13:47
  • @asgs - no, they are designed to be modified by the iterator. They are not designed to be modified both directly and via the iterator. Commented May 16, 2011 at 13:49
  • yea i was referring to the second part. Thanks for the confirmation. Commented May 16, 2011 at 13:50
  • 1
    Not to be modified while iterating, except by the current iterator. The iterator is allowed to remove the last returned element. Commented May 16, 2011 at 13:50
1

Iterators are fail-fast iterators so it may throw ConcurrentModificationException in your case.

The simplest solution would be;

instead of deleting items from list while iterating, add the ones that are going to be deleted to a new list and after the loop, you can remove all those using the removeAll method of list.

or vice versa, keep the ones you need to retain and assign the new list to the old one

answered May 16, 2011 at 13:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.