concurrentmodification

How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:56:46
问题 This question already has an answer here: Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 24 answers If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException . Is there any workaround? 回答1: 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

Why isn't this code causing a ConcurrentModificationException? [duplicate]

拜拜、爱过 提交于 2019-11-26 06:48:36
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 9 months ago . I was reading about ConcurrentModificationException and how to avoid it. Found an article. The first listing in that article had code similar to the following, which would apparently cause the exception: List<String> myList = new ArrayList<String>(); myList.add(\"January\"); myList.add(\"February\"); myList.add(\"March\"); Iterator<String> it = myList

Concurrent Modification exception

一曲冷凌霜 提交于 2019-11-26 00:38:15
问题 I have this little piece of code and it gives me the concurrent modification exception. I cannot understand why I keep getting it, even though I do not see any concurrent modifications being carried out. import java.util.*; public class SomeClass { public static void main(String[] args) { List<String> s = new ArrayList<>(); ListIterator<String> it = s.listIterator(); for (String a : args) s.add(a); if (it.hasNext()) String item = it.next(); System.out.println(s); } } 回答1: To avoid the

Getting a ConcurrentModificationException thrown when removing an element from a java.util.List during list iteration? [duplicate]

浪尽此生 提交于 2019-11-26 00:28:58
问题 This question already has answers here : Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop (24 answers) Closed 2 years ago . @Test public void testListCur(){ List<String> li=new ArrayList<String>(); for(int i=0;i<10;i++){ li.add(\"str\"+i); } for(String st:li){ if(st.equalsIgnoreCase(\"str3\")) li.remove(\"str3\"); } System.out.println(li); } When I run this code,I will throw a ConcurrentModificationException. It looks as though when I

Why am I not getting a java.util.ConcurrentModificationException in this example?

与世无争的帅哥 提交于 2019-11-26 00:08:14
问题 Note: I am aware of the Iterator#remove() method. In the following code sample, I don\'t understand why the List.remove in main method throws ConcurrentModificationException , but not in the remove method. public class RemoveListElementDemo { private static final List<Integer> integerList; static { integerList = new ArrayList<Integer>(); integerList.add(1); integerList.add(2); integerList.add(3); } public static void remove(Integer toRemove) { for(Integer integer : integerList) { if(integer

Why is a ConcurrentModificationException thrown and how to debug it

核能气质少年 提交于 2019-11-25 21:50:03
问题 I am using a Collection (a HashMap used indirectly by the JPA, it so happens), but apparently randomly the code throws a ConcurrentModificationException . What is causing it and how do I fix this problem? By using some synchronization, perhaps? Here is the full stack-trace: Exception in thread \"pool-1-thread-1\" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$ValueIterator.next(Unknown Source) at org.hibernate