java - compare treeset for equality -


i have 2 sets of hashset converted treeset sort ease of comparison. after converting hashset treeset. when compare these 2 treeset using 'equals' function, says different. debug it, shows same content same order. can not understand wrong ?

   public class testproductbundle {     @suppresswarnings("unused")     public static void main(string args[]) {          // hashset         set<classa> hashseta = new hashset<classa>() {             {                 add(new classa("name", 1, "desc"));                 add(new classa("name", 2, "desc"));                 add(new classa("name", 3, "desc"));             }         };          set<classa> hashsetb = new hashset<classa>() {             {                 add(new classa("name", 1, "desc"));                 add(new classa("name", 2, "desc"));                 add(new classa("name", 3, "desc"));             }         };          treeset<classa> treeseta = new treeset<classa>(new compareid()) {             {                 addall(hashseta);             }         };          treeset<classa> treesetb = new treeset<classa>(new compareid()) {             {                 addall(hashsetb);             }         };          if (treeseta.equals(treesetb))             system.out.println("equal set of tree");         else             system.out.println("unequal set of tree");   // result.     }} 

classa gives below:

class classa { string name; int id; string desc;  public classa(string name, int id, string desc) {     this.name = name;     this.id = id;     this.desc = desc; }      int getid() {         return id;     } }  class compareid implements comparator<classa> {     @override     public int compare(classa o1, classa o2) {         if (o1.getid() > o2.getid())             return 1;         else             return -1;     } } 

edit: tried if (treeseta.containsall(treesetb) && treesetb.containsall(treeseta) condition also. same result, "unequal set of tree"

your compare method allways returns unequality.

from doc:

[...]

a negative integer, zero, or positive integer first argument less than, equal to, or greater second.

[..]

public int compare(classa o1, classa o2) {     if (o1.getid() > o2.getid())         return 1;     else if(o2.getid() > o1.getid())         return -1;     // 0  indicates equality.     else return 0; } 

including results in output

equal set of tree 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -