Getting ConcurrentException while moving a list

I am in a very peculiar state. I have a list similar to below: -

List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); 

Now, when I perform several types of movements, for example, using the extended for, iterator and normal loop, the following code snippets are given: -

1> Advanced Loop: -

 try { for(String a : list) { System.out.println(a); list.add("f"); } } catch (Exception e) { e.printStackTrace(); } 

2> Iterator: -

 try { Iterator<String> itr = list.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); list.add("f"); } } catch (Exception e) { e.printStackTrace(); } 

3> Normal cycle: -

 for (int i=0;i<list.size();i++) { System.out.println(list.get(i)); list.add("f"); } 

Now, the special problem is that when using the extended for-loop and iterator, I get the following exception: -

 java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) the reason i know, that while iterating through a list, one cannot modify it parallely. 

but when I use the normal loop, it works correctly, am I missing something ??

Please, help!!!..

+4
source share
4 answers

If you modify the list, it will invalidate any Iterator objects that it creates. The advanced loop (1) is compiled in much the same code as the iterator loop (2), which means that there is an Iterator object created behind the scenes.

The javadocs for ConcurrentMOdificationException have more details.

If you want to add during iteration, use

 ListIterator listIter = ... while(listIter.hasNext()) { if(shouldAdd(iter.next())) { iter.add(....) } } 
+7
source

An extended loop is just syntactic sugar for an iterator. The iterator always calls checkForComodification() whenever it runs next() . This method throws an exception if it sees that you have changed the list.

Running "Normal Loop" does not provide this function for checking the number of modifications, because you do not use an iterator for the loop, you strictly use the for loop construct.

+3
source

Running a for loop on a list will cause serious problems. You will reread the records (deleting the previous record may cause this), and you can get IndexOutOfBoundsExceptions. Use CopyOnWriteArrayList for a security-compatible list with not too much overhead, depending on your application. If you have many entries in the list, use ConcurrentLinkedQueue , but keep in mind that it acts as a queue, and you can only queue things with it as soon as adding to the end and removing from the front.

0
source

You should keep in mind that ConcurrentModificationException is thrown by the Iterator method, but not by the List add method. This means that the list actually changes when a new item is added to the loop. If you exit the loop after adding a new item, an exception is not thrown and the item is added to the list.

Otherwise, you can use the Iterator add and remove method to modify the list inside the loop.

0
source

Source: https://habr.com/ru/post/1347842/


All Articles