java - Yet another LinkedHashSet .equals dilema -


i know has been asked several times , promise have read on 100 articles before resorting posting. here issue:

  1. i have created own object (workoutdata). made of several string variables, , integer , linked list of strings:

    public class workoutdata implements serializable {     private static final long serialversionuid = 878893767880469289l;     string workoutlocation;     string bodypart;     string exercise;     int numofsets;     linkedlist<linkedlist<string>> datessetsandreps; } 
  2. in case duplicates ever datafile reading objects linkedhashset. thinking automatically eliminate duplicates.

of course isn't working, because each object has own id , therefore not "equal" object. try around have overridden equals , hashcode inside workoutdata class.

for reason still not working. here how loading linkedhashset:

for (int iterator = 0; iterator < numberofworkouts; iterator++)     tempworkoutdataset.add((workoutdata) in.readobject()); 

here overridden methods in workoutdata class:

@override public boolean equals(object other) {     if (other == null)     {         return false;     }      if (this.getclass() != other.getclass())     {         return false;     }      if (!this.workoutlocation.equals(((workoutdata)other).workoutlocation))     {         return false;     }      if (!this.bodypart.equals(((workoutdata)other).bodypart))     {         return false;     }      if (!this.exercise.equals(((workoutdata)other).exercise))     {         return false;     }      return true; }  public int hashcode() {     return this.workoutlocation.length(); } 

the hashset contains duplicates no matter try. think equals method correct, freely admit have no idea hashcode method meant accomplish.

this question not duplicate of "how compare strings in java?". question talks == vs .equals issue. issue has more overriding hashcode. think problem lies. couple fact still unclear hashcode override meant accomplish.

thanks great out there able resolve question. amounted 2 basic issue:

  1. overriding equals (as opposed trying ==) right thing keep duplicates being added hashset. since objects have numeric identifiers == not work , have use equals. if equals not overriden hashset use system supplied equals pretty acts same ==. problem thing trying away using in base code, ==, mistakenly used in equals override in object class file. once changed equals override use !equals (as shown above) instead of != code worked planned.

  2. overriding hashcode important. override useless because returned value not unique in (or most) cases. following of links provided configured hashcode override methodology used same logic equals override, suggested. shown above. note how same 3 parameters of object used in equals override used in hashcode override.

now code works fine. concept of overriding equals , hashcode objects in hashset complicated @ first, seems rather easy upon reflection. again great help!


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -