title: java TreeSet: comparing and equality
body: <p>I'd like to have list of object sorted with property 'sort_1'. But when I want to remove I'd like it to use property 'id'. The following code represents the problem.</p>
  
  <pre><code>package javaapplication1;
  
  import java.util.TreeSet;
  
  public class MyObj implements Comparable&lt;MyObj&gt; {
      public long sort_1;
      public long id;
  
      public MyObj(long sort, long id) {
          this.sort_1=sort;
          this.id=id;
      }
  
      @Override
      public int compareTo(MyObj other) {        
          int ret = Long.compare(sort_1, other.sort_1);               
          return ret;
      }    
  
      public String toString() {
          return id+":"+sort_1;
      }
  
      public static void main(String[] args) {
          TreeSet&lt;MyObj&gt; lst=new TreeSet&lt;MyObj&gt;();
  
                  MyObj o1 = new MyObj(99,1);
          MyObj o2 = new MyObj(11,9);
  
          lst.add(o1);
          lst.add(o2);    
  
          System.out.println(lst);
  
                  MyObj o3 = new MyObj(1234, 1);
                  //remove myObje with id 1
                  boolean remove=lst.remove(o3);
  
                  System.out.println(lst);
      }
  
  }
  </code></pre>
  
  <p>Output of this code is:</p>
  
  <pre><code>[9:11, 1:99]
  [9:11, 1:99]
  </code></pre>
  
  <p>I need to have list sorted as I do a lot of additions to the list. I don't want to explicitly use any 'sort' method. What are my options ?</p>
  
  <p>EDIT:</p>
  
  <p>My requirement is to have: objects with 'id' as unique but there can be object's with duplicate 'sort'  value.</p>