If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException. Is there any workaround?
If I modify a Collection while iterating over it using for-each loop, it gives
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
Iterator#remove.This is the only safe way to modify a collection during iteration. For more information, see The Collection Interface tutorial.
If you also need the ability to add elements while iterating, use a
ListIterator.