title: java.util.ConcurrentModificationException in iterating over TreeSet
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/602636/concurrentmodificationexception-and-a-hashmap">ConcurrentModificationException and a HashMap</a>  </p>
  </blockquote>
  
  
  
  <p>I am getting the following exception </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>while executing the following code</p>
  
  <pre><code>public void makeConnected() {
          TreeSet&lt;Node&gt; exploredNodes = new TreeSet&lt;Node&gt;();
          TreeSet&lt;Node&gt; unexploredNodes = new TreeSet&lt;Node&gt;();
  for (Node n : unexploredNodes) {
          ...
          exploredNodes.add(n);
          unexploredNodes.remove(n);
          ...
  }
  </code></pre>
  
  <p>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</p>