title: Is it better to use a TreeSet or ArrayList when using a custom comparator
body: <p>I have implemented a graph.
  I want to sort a given subset of vertices with respect to their degrees. 
  Therefore, I've written a custom comparator named <code>DegreeComparator</code>.</p>
  
  <pre><code>private class DegreeComparator implements Comparator&lt;Integer&gt;
  {
      @Override
      public int compare(Integer arg0, Integer arg1) 
      {
          if(adj[arg1].size() == adj[arg0].size()) return arg1 - arg0;
          else return adj[arg1].size() - adj[arg0].size());
      }
  
  }
  </code></pre>
  
  <p>So, which one of the below is more efficient?</p>
  
  <p><strong>Using <code>TreeSet</code></strong>  </p>
  
  <pre><code>public Collection&lt;Integer&gt; sort(Collection&lt;Integer&gt; unsorted)
  {
      Set&lt;Integer&gt; sorted = new TreeSet&lt;Integer&gt;(new DegreeComparator());
      sorted.addAll(unsorted);
      return sorted;
  }
  </code></pre>
  
  <p><strong>Using <code>ArrayList</code></strong></p>
  
  <pre><code>Collections.sort(unsorted, new DegreeComparator());
  </code></pre>
  
  <p>Notice that the second approach is not a function, but a one-line code.</p>
  
  <p>Intuitively, I'd rather choose the second one. But I'm not sure if it is more efficient.</p>