title: Controller ordering in TreeSet to List conversion
body: <p>I have 2 lists coming into my method sorted in a certain order (either ascending or descending based on an ID field)</p>
  
  <p>I have a custom comparator implementation that allows me to throw both lists into the treeset and achieve the desired results.</p>
  
  <p>My issue is after the treeset is loaded, I need to return a single list back from the method.  My first implementation didn't care about ordering so I did this (composite is my named TreeSet):</p>
  
  <pre><code>    composite.addAll(custom);
      composite.addAll(reference);    
  
      Iterator&lt;MyObject&gt; anIter = composite.iterator();
      ArrayList&lt;MyObject&gt; returnVal = new ArrayList&lt;MyObject&gt;();
      while(anIter.hasNext()) 
          returnVal.add(anIter.next());
  </code></pre>
  
  <p>Once I execute this, the two lists "custom" and "reference" are back to the default ordering.  The Javadocs for Collection's iterator() method state that will return the list in ascending order, which is likely where my trouble is coming from.</p>
  
  <p>So...is there no way to return the content of a TreeSet while protecting the original list ordering?  A treeset is what came to mind because I wanted to use the power of the comparator interface over the two collections to union them with addAll() and throw out the dupes.</p>
  
  <p>Any suggestions on protecting the ordering would be appreciated.</p>
  
  <p><em><strong>EDIT</em>*</strong></p>
  
  <pre><code>Set&lt;MyObject&gt; composite = new TreeSet&lt;MyObject&gt;(new Comparator&lt;MyObject&gt;(){
          @Override
          public int compare(MyObject arg0, MyObject arg1) {
              int arg0ID = arg0.getObjID();
              int arg1ID = arg1.getObjID();                
              if(arg0ID &lt; arg1ID) return -1;
              else if(arg0ID&gt; arg1ID) return 1;
              else return 0;
          }           
      }); 
  </code></pre>