title: iterating over sorted treemap: java.util.ConcurrentModificationException
body: <p>I have basic knowledge in Java and working currently on a Java based code.</p>
  
  <p>EDIT: I didn't write the code</p>
  
  <p>I am iterating over a sorted treemap of <code>Event</code> Objects and getting this exception when I am trying to get the next element:</p>
  
  <p><code>java.util.ConcurrentModificationException
      at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
      at java.util.TreeMap$KeyIterator.next(Unknown Source)</code></p>
  
  <p>I think this should be due to the existence of mutiple entries with same value that are merged by the iterator comparator (be refering to this <a href="https://stackoverflow.com/questions/26304796/java-util-nosuchelementexception-on-iterating-over-sorted-treemap">question</a>, but I don't know how to find the keys used in the comparator. The object <code>Event</code> has many parameters (like id, time, etc)but not sure which one is used for the iterator.</p>
  
  <p>Here is the related code portion (exception in the second <code>SimEvent first = fit.next();</code>):</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>    if (future.size() &gt; 0) {
          List&lt;SimEvent&gt; toRemove = new ArrayList&lt;SimEvent&gt;();
          Iterator&lt;SimEvent&gt; fit = future.iterator();
          queue_empty = false;
          SimEvent first = fit.next();
          processEvent(first);
          future.remove(first);
  
          fit = future.iterator();
  
          // Check if next events are at same time...
          boolean trymore = fit.hasNext();
          while (trymore) {
              SimEvent next = fit.next();
              if (next.eventTime() == first.eventTime()) {
                  processEvent(next);
                  toRemove.add(next);
                  trymore = fit.hasNext();
              } else {
                  trymore = false;
              }
          }
  
          future.removeAll(toRemove);
  
      } else {...}
  </code></pre>
  
  <p>EDIT: The hall code of <code>future</code> class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class FutureQueue {
  
      /** The sorted set. */
      private final SortedSet&lt;SimEvent&gt; sortedSet = new TreeSet&lt;SimEvent&gt;();
  
      /** The serial. */
      private long serial = 0;
  
      /**
       * Add a new event to the queue. Adding a new event to the queue preserves the temporal order of
       * the events in the queue.
           * 
       * @param newEvent The event to be put in the queue.
       */
      public void addEvent(SimEvent newEvent) {
          newEvent.setSerial(serial++);
          sortedSet.add(newEvent);
      }
  
      /**
      * Add a new event to the head of the queue.
      * 
      * @param newEvent The event to be put in the queue.
      */
      public void addEventFirst(SimEvent newEvent) {
          newEvent.setSerial(0);
          sortedSet.add(newEvent);
      }
  
      /**
       * Returns an iterator to the queue.
       * 
       * @return the iterator
       */
       public Iterator&lt;SimEvent&gt; iterator() {
          return sortedSet.iterator();
       }
  
      /**
       * Returns the size of this event queue.
       * 
       * @return the size
       */
      public int size() {
          return sortedSet.size();
      }
  
      /**
       * Removes the event from the queue.
       * 
       * @param event the event
       * @return true, if successful
       */
      public boolean remove(SimEvent event) {
          return sortedSet.remove(event);
      }
  
      /**
       * Removes all the events from the queue.
       * 
       * @param events the events
       * @return true, if successful
       */
      public boolean removeAll(Collection&lt;SimEvent&gt; events) {
          return sortedSet.removeAll(events);
      }
  
  
      public void clear() {
          sortedSet.clear();
      }
  }
  </code></pre>
  
  <p>Any suggestion on how to proceed to debug this problem?</p>