java TreeSet: comparing and equality -


i'd have list of object sorted property 'sort_1'. when want remove i'd use property 'id'. following code represents problem.

package javaapplication1;  import java.util.treeset;  public class myobj implements comparable<myobj> {     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<myobj> lst=new treeset<myobj>();                  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 id 1                 boolean remove=lst.remove(o3);                  system.out.println(lst);     }  } 

output of code is:

[9:11, 1:99] [9:11, 1:99] 

i need have list sorted lot of additions list. don't want explicitly use 'sort' method. options ?

edit:

my requirement have: objects 'id' unique there can object's duplicate 'sort' value.

just chance found out yesterday well. seems artifact of implementation of treemap (which treeset uses store entries).

treemap uses binary search tree storing key/value pairs, ever uses given comparator (or compare function if key class implements comparable) check equality, can see in code exxcerpt:

final entry<k,v> getentry(object key) {     // offload comparator-based version sake of performance     if (comparator != null)         return getentryusingcomparator(key);     if (key == null)         throw new nullpointerexception();     @suppresswarnings("unchecked")         comparable<? super k> k = (comparable<? super k>) key;     entry<k,v> p = root;     while (p != null) {         int cmp = k.compareto(p.key);         if (cmp < 0)             p = p.left;         else if (cmp > 0)             p = p.right;         else             return p;     }     return null; } 

i'd call (not fixable) bug since javadoc of comparable interface explicitly says returning 0 compareto function not have imply "equalness":

it recommended, not strictly required (x.compareto(y)==0) == (x.equals(y)).

you won't able store stuff in treeset way want to. i'd recommend using normal hashmap or linkedhashmap , sorting output when need sort collections.sort.

besides of this, find strange implement comparable interface. things don't have "natural" ordering obvious. can lead strange bugs (like one!), typically sort when need using custom comparators. java 8 makes writing easy well!


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -