• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrent Modification Exception during delete loop

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a java.util.ConcurrentModificationException during this code:



The line throwing the exception i think is:



The API says basically that this is caused by removing an item while iterating through a fail-fast collection. Not sure what this means, or how I can fix it. Any ideas?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for this error isn't very good, unfortunately. The problem is that you are calling remove() directly on the collection while you are iterating that collection. If you want to modify a collection while iterating through it, you should use methods of the iterator, not of the collection. In this case you don't see the Iterator, because it's hidden by the for loop. However to do what you want, you will have to replace the loop with an old-fashioned iteration loop, so that you can see the iterator:

Here the key point is that we're no longer using getPatients().remove(patient) - we're using it.remove(). The Iterator knows how to safely remove objects while iterating. The Collection doesn't.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic