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
.
More Related Contents:
- Why is a ConcurrentModificationException thrown and how to debug it
- When to use LinkedList over ArrayList in Java?
- Java 8 Distinct by property
- How can I turn a List of Lists into a List in Java 8?
- How to convert int[] into List in Java?
- How can I initialise a static Map?
- How to count the number of occurrences of an element in a List
- Which is more efficient, a for-each loop, or an iterator?
- Which Java Collection should I use?
- Convert an array of primitive longs into a List of Longs
- Is there a common Java utility to break a list into batches?
- How can I convert int[] to Integer[] in Java?
- Sorted collection in Java
- How can I sort a List alphabetically?
- Why can Java Collections not directly store Primitives types?
- Immutable vs Unmodifiable collection [duplicate]
- Identify duplicates in a List
- Removing items from a list [duplicate]
- Collection Interface vs arrays
- How to remove element from ArrayList by checking its value?
- Most efficient Java primitive collections library [closed]
- Google Guava vs. Apache Commons [closed]
- What is the fastest Java collection with the basic functionality of a Queue?
- Remove empty collections from a JSON with Gson
- Map that could be iterated in the order of values
- Why does TreeSet throw a ClassCastException?
- Shortcut for adding to List in a HashMap
- ArrayList vs. Vectors in Java if thread safety isn’t a concern
- How does ConcurrentHashMap work internally?
- Vector vs Collections.synchronizedList(ArrayList)