title: How TreeSet checks for Duplicates
body: <p>I am checking how TreeSet checks for duplicate elements and have the following code</p>
  
  <pre><code>  import java.util.*;
  
    public class TreeDemo{
  
      public static void main(String[] args)
          {
              new TreeDemo().go();
          }
  
      public void go()
      {
          Song s1 = new Song("song1","artist1");
          Song s2 = new Song("song2","artist2");
          Song s3 = new Song("song3","artist3");
          Song s4 = new Song("song3","artist3");
  
          Set&lt;Song&gt; tree = new TreeSet&lt;Song&gt;();
  
          tree.add(s1);
          tree.add(s2);
          tree.add(s3);
          tree.add(s4);
  
          System.out.println(tree);
  
      }
  }
  
  class Song implements Comparable&lt;Song&gt;{
      private String title;
      private String artist;
  
      public Song(String t, String a)
      {
          title=t;
          artist=a;
      }
  
      public String getTitle(){
          return title; 
      }
  
      public int compareTo(Song s){
          //Song s = (Song)o;
          return title.compareTo(s.getTitle());
      }
  
  public String toString(){
      return title;
  }
  
  }
  </code></pre>
  
  <p>When I execute this code, I get the following output</p>
  
  <pre><code>[song1, song2, song3]
  </code></pre>
  
  <p>My question is:-</p>
  
  <ul>
  <li>Even if I haven't implemented hashCode and equals method (I did implement the Comparable interface since its mandatory and needed to keep the Set sorted), how did TreeSet determine the duplicates?</li>
  <li>Did it use Object class default implementation? It looks like it used "title" field for this check since when I add  treats it as duplicate but when I add  it doesn't treats it as duplicate.</li>
  </ul>
  
  <p>Thanks.</p>