Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, can you modify a List while iterating through it?

Tags:

java

arraylist

I understand that in Java a Collection<E> should not be modified while iterating through it, such as removing or adding elements. But what about changing the elements in a List? For example, what if we have

List<String> letters = new ArrayList<String>(); letters.add("A"); letters.add("B"); letters.add("C"); int i = 0;  for (String letter : letters) {     letters.set(i, "D");     i++; } 

So, I'm not talking about modifying the object stored at an element; I'm talking about changing what the object is. The size of the List is not being changed, but the object at the index is changing, so technically the List is being modified. My boss claims this code is fine (and it does appear to work), but I still don't think it is correct. Would a different way of doing it, maybe using the set(E e) method of a ListIterator, be better?

like image 410
user176410 Avatar asked Dec 17 '13 16:12

user176410


People also ask

Can we modify list while iterating in Java?

The Best Answer is At the end the whole list will have the letter "D" as its content. It's not a good idea to use an enhanced for loop in this case, you're not using the iteration variable for anything, and besides you can't modify the list's contents using the iteration variable.

Can I modify list while iterating?

The general rule of thumb is that you don't modify a collection/array/list while iterating over it. Use a secondary list to store the items you want to act upon and execute that logic in a loop after your initial loop.

Can you add to a list while iterating Java?

You can't modify a Collection while iterating over it using an Iterator , except for Iterator.

What happens if you modify the value of an element in a list while iterating using iterators?

The size of the List is not being changed, but the object at the index is changing, so technically the List is being modified.


2 Answers

There is nothing wrong with the idea of modifying an element inside a list while traversing it (don't modify the list itself, that's not recommended), but it can be better expressed like this:

for (int i = 0; i < letters.size(); i++) {     letters.set(i, "D"); } 

At the end the whole list will have the letter "D" as its content. It's not a good idea to use an enhanced for loop in this case, you're not using the iteration variable for anything, and besides you can't modify the list's contents using the iteration variable.

Notice that the above snippet is not modifying the list's structure - meaning: no elements are added or removed and the lists' size remains constant. Simply replacing one element by another doesn't count as a structural modification. Here's the link to the documentation quoted by @ZouZou in the comments, it states that:

A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification

like image 141
Óscar López Avatar answered Sep 17 '22 18:09

Óscar López


Use CopyOnWriteArrayList
and if you want to remove it, do the following:

for (Iterator<String> it = userList.iterator(); it.hasNext() ;) {     if (wordsToRemove.contains(word))     {         it.remove();     } } 
like image 23
ThmHarsh Avatar answered Sep 18 '22 18:09

ThmHarsh