java - How do I say that an object is present in a Set that also has a treeset? -


i have s set of object a.

class {      string text1;     string text2     treeset<classaa> classaaset;     @override      public boolean equals(object b){      }     @override      public int hashcode(){      } } 

the other class:

class aa {     string y;     string z;      @override     public int hashcode() {         return objects.hash(y,z)     }      @override     public int compareto(classaa other) {         return y.compareto(other.y)     } } 

i have set of a's , single object a. how set<a>aset.contains(a)?

internally, since have treeset, ignoring equals implementation.

it clearer if posted code correct extent compiled. anyway:

your class aa should fulfil 3 requirements: (1) equal objects should return same hashcode(), (2) compareto() should consistent equals(), , (3) compareto() , treeset work correctly, class should implement comparable<aa>.

i hope can add implements clause (3) on own. (2), @ documentation of comparable. easiest fix override equals() aa equal aa same y. unfortunately, break (1) because current hashcode method depends on z too.

so decide means 2 aa objects equal. in other words, given classaaset in a , aa object, means set contains such aa. enough have same y or should have both same y , same z? code equals(), hashcode() , compareto() reflect decision.

actually, believe treeset not use hashcode(), in dirty solution live without fulfilling (1), without overriding hashcode() @ all. in trouble if class used in new context other time, wouldn’t recommend it.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -