This is not a synchronization problem. This will occur if the underlying collection that is being iterated over is modified by anything other than the Iterator itself.
Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Entry item = it.next(); map.remove(item.getKey()); }
This will throw a ConcurrentModificationException
when the it.hasNext()
is called the second time.
The correct approach would be
Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Entry item = it.next(); it.remove(); }
Assuming this iterator supports the remove()
operation.
Related Posts:
- What is a StackOverflowError?
- When to use LinkedList over ArrayList in Java?
- Difference between Arrays.asList(array) and new ArrayList
(Arrays.asList(array)) - What is the proper way to handle a NumberFormatException when it is expected?
- Exception in thread “main” java.util.NoSuchElementException
- Initialization of an ArrayList in one line
- How to sort an ArrayList?
- JavaFX – Exception in Application start method?
- Difference between HashSet and HashMap?
- JavaFX – Exception in Application start method? [duplicate]
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- EOFException – how to handle?
- Why is there no SortedList in Java?
- Can I catch multiple Java exceptions in the same catch clause?
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- Printing HashMap In Java
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- HashSet vs. ArrayList
- FIFO based Queue implementations?
- Create a List of primitive int?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- How to create a custom exception type in Java?
- How to convert int[] to Integer[] in Java?
- Change priorityQueue to max priorityqueue
- Collections.emptyMap() vs new HashMap()
- Sort a Map
by values - Exception in thread “main” java.lang.ArithmeticException: / by zero
- Java can’t find file when running through Eclipse
- FXML Load exception
- StringIndexOutOfBoundsException String index out of range: 0
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- Connection Java – MySQL : Public Key Retrieval is not allowed
- How to sort a HashSet?
- Union or intersection of Java Sets
- Throwing multiple exceptions in Java
- java.lang.ArrayIndexOutOfBoundsException: 0
- Official reasons for “Software caused connection abort: socket write error”
- How can I read a text file in Android?
- java.util.ConcurrentModificationException with iterator
- Exception is never thrown in body of corresponding try statement
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- How do I “decompile” Java class files? [closed]
- How do I compare strings in Java?
- Could not reserve enough space for object heap
- What does “Could not find or load main class” mean?
- What is an illegal reflective access?
- Convert Set to List without creating new List
- Please initialize the log4j system properly warning
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- Java – Best way to print 2D array?
- How to update a value, given a key in a hashmap?
- How to schedule a periodic task in Java?
- What is Serialization?
- SSL and cert keystore
- What’s the difference between import java.util.*; and import java.util.stream;?
- How do I efficiently iterate over each entry in a Java Map?
- Collections sort(List
,Comparator super T>) method example - Login Application with 1 stage and multiple scene in JavaFX
- Handle mouse event anywhere with JavaFX
- What is the meaning of “%d:%02d” in `printf`?
- Type List vs type ArrayList in Java
- Maven error : Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher
- Polymorphism vs Overriding vs Overloading
- Difference between Static methods and Instance methods
- Java The method is undefined for this type
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- How to check if the value is integer in java?
- Java Error: Invalid top level statement
- How to make an array of arrays in Java
- Instantiating object of type parameter
- Selenium — How to wait until page is completely loaded
- Cannot resolve symbol ‘button’ -Android Studio
- Checking to see if array is full
- Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
- “The import org.springframework cannot be resolved.”
- How to synchronize or lock upon variables in Java?
- why f is placed after float values?
- Syntax error on token “;”, { expected after this token in Random string creator
- Chrome Error: You are using an unsupported command-line flag: –ignore-certifcate-errors. Stability and security will suffer
- Configure active profile in SpringBoot via Maven
- Is there any way to program Java on Visual Studio 2015
- Keylistener not working for JPanel
- the getSource() and getActionCommand()
- Failed to install android-sdk: “java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema”
- Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven
- Regex pattern including all special characters
- Using while loop as input validation [duplicate]
- java -version and javac -version showing different versions
- when spring boot startup,throw out the “method names must be tokens” exception
- How to get javax.comm API?
- how to convert exe file to .jar file? [duplicate]
- How to print a table of information in Java