title: java : TreeSet Collection and Comparable interface
body: <p>I have following code :
  I am trying to insert Item object in TreeSet and I am not getting desire output.</p>
  
  <pre><code>public class Main
  {
      public static void main(String a[])
      {
          Item i1=new Item(1,"aa");
          Item i2=new Item(5,"bb");
          Item i3=new Item(10,"dd");
          Item i4=new Item(41,"xx");
          Item i5=new Item(3,"x5");
          TreeSet t=new TreeSet();    
          t.add(i1);
          t.add(i2);
          t.add(i3);
          t.add(i4);
          t.add(i5);
          System.out.println(t);      
      }
  }
  class Item implements Comparable&lt;Item&gt;
  {
      String  nm;
      int price;
      public Item(int n,String nm)
      {
          this.nm=nm;
          price=n;
      }
      public int compareTo(Item i1)
      {
          if(price==i1.price)
              return 0;
          else if(price&gt;=i1.price)
              return 1;
          else
              return 0;
      }
      public String  toString()
      {
          return "\nPrice "+price+" Name : "+nm;
      }    
  }
  </code></pre>
  
  <p>Output :</p>
  
  <p>[ Price 1 Name : aa,<br/>
  Price 5 Name : bb,<br/>
  Price 10 Name : dd,<br/>
  Price 41 Name : xx ] <br/></p>
  
  <p><code>Item i5=new Item(3,"x5");</code> is not Inserted why?<br/>
  Why I can do to insert in TreeSet.</p>