title: Using iterator on a TreeSet
body: <p><strong>SITUATION:</strong> I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. </p>
  
  <pre><code>TreeSet&lt;Custom&gt; ts=new TreeSet&lt;Custom&gt;();
  Iterator&lt;Custom&gt; itr=ts.iterator();
  while(itr.hasNext()){
      Custom c=itr.next();
      //Code to add a new element to the TreeSet ts
  }
  </code></pre>
  
  <p><strong>QUESTION:</strong> Well I want to know that if I add a new element to the TreeSet within the while loop, then will that new element get sorted immediately. In other words, if I add a new element within the while loop and it is less than the one which I am currently holding in c, then in the next iteration will I be getting the same element in c as in the last iteration?(since after sorting, the newly added element will occupy a place somewhere before the current element).</p>