Possible Duplicate:
ConcurrentModificationException and a HashMap
I am getting the following exception
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
at java.util.TreeMap$KeyIterator.next(Unknown Source)
at Types$AdjList.makeConnected(Types.java:281)
at Main.main(Main.java:56)
while executing the following code
public void makeConnected() {
TreeSet<Node> exploredNodes = new TreeSet<Node>();
TreeSet<Node> unexploredNodes = new TreeSet<Node>();
for (Node n : unexploredNodes) {
...
exploredNodes.add(n);
unexploredNodes.remove(n);
...
}
I am not using the iterator like in HashMap but need to use a Set that may grow or reduce based on some condition.
I will accept and give points to all answers. Look forward to how to replies on how to solve this problem of ConcurrentModificationException
Thanks,
Somnath
The for loop internally uses iterators and you are not removing the elements using the iterator. Hence, the problem. Use iterator’s remove method as below: