I want to avoid getting ConcurrentModificationException
. How would I do it?
Create a new list, and populate that one.
List<MyType> originalList = ...
List<MyType> newList = new ArrayList<>(originalList);
for(MyType type : originalList)
{
// Some decisions
newList.add(new MyType());
}
There are three approaches to avoid above exception
You can convert the list to an array and then iterate on the array. This approach works well for small or medium size list but if the list is large then it will affect the performance a lot.
You can lock the list while iterating by putting it in a synchronized block. This approach is not recommended because it will cease the benefits of multithreading.
If you are using JDK1.5 or higher then you can use ConcurrentHashMap and CopyOnWriteArrayList classes. It is the recommended approach.
Instead of using an iterator, you can use a for loop with an index. For example:
int originalLength = list.length();
for (int i = 0; i < originalLength; i++) {
MyType mt = list.get(i);
//... processing
//... insertions
}
You may use a ListIterator
which has support for a remove/add method during the iteration itself.
ListIterator<Book> iter = books.listIterator();
while(iter.hasNext()){
if(iter.next().getIsbn().equals(isbn)){
iter.add(new Book(...));
}
}
You want to use a ListIterator
. You can get one of these from any kind of list, though for efficiency you probably want to get one from a LinkedList
.
import java.util.*;
class TestListIterator {
public static void main(String[]args) {
List<Integer> L = new LinkedList<Integer>();
L.add(0);
L.add(1);
L.add(2);
for (ListIterator<Integer> i = L.listIterator(); i.hasNext(); ) {
int x = i.next();
i.add(x + 10);
}
System.out.println(L);
}
}
Prints [0, 10, 1, 11, 2, 12]
.