Exception in thread main java.util.ConcurrentModificationException
Answer: If you are getting below exception:
Exception in thread "main" java.util.ConcurrentModificationException
It means you are trying to modify collection while iterating it. By design all collection classes in java.util.package are fail fast and we get exception.
To fix this issue you could do below:
Don’t try to modify direct collection itself use iterator remove method if you are removing element form collection. Please see example below:
If your requirement is different where iterator method does not support it then go for java.util.concurrent package classes to do modification work. In below example will show you how to use ConcurrentHashMap and CopyOnWriteArrayList class form java.util.concurrent package.