title: Can I sort ArrayList of ArrayList simply by using TreeSet?
body: <p>=== MAJOR EDIT (and a minor one) === </p>
  
  <p>I've completely rewritten the code I submitted a couple of days ago since it was based on two others' not-so-well-thought-out hunks of code, a very inappropriate idea from the gitgo. My bad for failing to research enough.</p>
  
  <p>Nevertheless, @F. Böller was kind enough to Answer the question about my original code and said I needed to define a <code>comparator</code> in this context:
  <code>TreeSet&lt;ArrayList&lt;Integer&gt;&gt; hs = new TreeSet&lt;&gt;(comparator);</code>. </p>
  
  <p>That was all the encouragement I needed to Google <a href="http://www.java2novice.com/java-collections-and-util/treeset/comparator-object/" rel="nofollow">java treesort comparator</a> and based my revision on what I saw there. It wasn't at all easy and I confess I still don't quite know WHY the syntax below turned out to largely be all I needed, but I'd've never come up with it without the example at the Googled link above.</p>
  
  <p><code>TreeSet&lt;Leaf&gt; theTreeSet = new TreeSet&lt;&gt;(new HowToCompareLeaves());</code> </p>
  
  <p>=== minor EDIT-- THE FOLLOWING LINE IS WRONG:</p>
  
  <p>Anyway, the code below works great (sorts based on 1st element in each "leaf"). </p>
  
  <p>=== THE FOLLOWING LINE IS CORRECT:</p>
  
  <p>Anyway, the code below works great (sorts and compares ALL elements in each "leaf"). </p>
  
  <p>=== end minor edit ===</p>
  
  <p>However, I'd appreciate it if someone could <strong>explain (to someone who'd never used <code>TreeSet</code> before) what the line above actually does. It seems to define a new instance of what's defined as the method for comparing the data items that <code>ar</code> is composed of... Is that it?? Is it a constructor for the new <code>TreeSet</code> that is required to define how it sorts new elements into itself??</strong> </p>
  
  <pre><code>package treed;
  import java.util.ArrayList;
  import java.util.Comparator;
  import java.util.TreeSet;
  
  public class TreeSetExample {
  
    public static final int NR = 4, NC = 3;
  
    public static void main(String a[])
    {
      ArrayList&lt;Leaf&gt; ar = new ArrayList&lt;&gt;(NR);
  
      for (int i = 0; i &lt; NR; i++)
       ar.add(new Leaf());
  
      System.out.println("Pre-sort:");
      for(int i = 0; i &lt; NR; i++)
        System.out.println(ar.get(i));
  
      TreeSet&lt;Leaf&gt; theTreeSet = new TreeSet&lt;&gt;(new HowToCompareLeaves());
  
      System.out.println("\nPost-sort:");
      for (int i = 0; i &lt; NR; i++)
        theTreeSet.add(ar.get(i));
  
      for(Leaf e:theTreeSet)           
        System.out.println(e);
    }
  }
  
  class HowToCompareLeaves implements Comparator&lt;Leaf&gt;
  {
    @Override 
    public int compare(Leaf e, Leaf f) 
    {
      if(e.row.get(0) &gt; f.row.get(0)) return  1;
      if(e.row.get(0) &lt; f.row.get(0)) return -1;
      return 0;
    }
  }    
  
  class Leaf
  {
      ArrayList&lt;Integer&gt; row;  
  
      Leaf()
      {
        row = new ArrayList&lt;&gt;(TreeSetExample.NC);
        for (int i = 0; i &lt; TreeSetExample.NC; i++)
          row.add((int)(Math.random()*89 + 10));
      }
  
    public String toString()
    {
      String s = "";
      for (int i = 0; i &lt; TreeSetExample.NC; i++) {
        s += row.get(i) + "...";
      }
      return s;
    }
  }
  </code></pre>
  
  <p>=== end of EDIT; rest is trash =======================</p>
  
  <p>I was following a new unanswered question about an ArrayList of ArrayList that needed un-dupe-ing and sorting. One answer suggested using a <code>HashSet</code> to enable removing duplicates, but that doesn't sort, as the working code below confirms.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashSet;
  
  public class TwoDimArrayList {
  
    static           ArrayList&lt;Integer&gt; r;
    static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; a2D = new ArrayList&lt;&gt;();
  
    public static void main(String[] args) {
      for (int i = 0; i &lt; 5; i++) {
        r = new ArrayList&lt;&gt;();
        for (int j = 0; j &lt; 3; j++)
          r.add((int)(Math.random() * 89 + 11));
        a2D.add(r);
      }
      for (int i = 0; i &lt; a2D.size(); i++){
        for (int j = 0; j &lt; a2D.get(i).size(); j++) 
          System.out.print(a2D.get(i).get(j) + "\t");
        System.out.println("...");
      }
  
      HashSet&lt;ArrayList&lt;Integer&gt;&gt; hs = new HashSet&lt;&gt;(a2D.size());
  
      for (int i = 0; i &lt; a2D.size(); i++)
        hs.add(a2D.get(i));
  
      a2D.clear();
  
      for (int i = 0; i &lt; hs.size(); i++)
        a2D.add((ArrayList&lt;Integer&gt;) hs.toArray()[i]);
  
      for (int i = 0; i &lt; a2D.size(); i++){ 
        for (int j = 0; j &lt; a2D.get(i).size(); j++)
          System.out.print(a2D.get(i).get(j) + "\t");
        System.out.println();
      }
    }
  }
  </code></pre>
  
  <p>I tried simply changing <code>HashSet</code> to <code>TreeSet</code> since the latter provides sorting:</p>
  
  <pre><code>TreeSet&lt;ArrayList&lt;Integer&gt;&gt; hs = new TreeSet&lt;&gt;(a2D.size());
  </code></pre>
  
  <p>That gave error: <code>cannot infer arguments for TreeSet</code>. So I eventually got rid of it like so:</p>
  
  <pre><code>TreeSet&lt;ArrayList&lt;Integer&gt;&gt; hs = new TreeSet&lt;&gt;();
  </code></pre>
  
  <p>But I get error on this line:</p>
  
  <pre><code>  hs.add(a2D.get(i));
  </code></pre>
  
  <p><code>Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable</code></p>
  
  <p>Is there some easy fix to this? Or is hoping that <code>TreeSet</code> will do the sorting in and of itself just wrong? (I'm new to sets, in case anyone was wondering...)</p>