Remove elements from collection while iterating
Let me give a few examples with some alternatives to avoid a ConcurrentModificationException. Suppose we have the following collection of books List<Book> books = new ArrayList<Book>(); books.add(new Book(new ISBN(“0-201-63361-2”))); books.add(new Book(new ISBN(“0-201-63361-3”))); books.add(new Book(new ISBN(“0-201-63361-4”))); Collect and Remove The first technique consists in collecting all the objects that we want to delete (e.g. using an … Read more