java - Why does this String.equals() method always return false? -


i have class have implemented custom hashcode() , equals(object) method using eclipse method generator. each object of class has string field called muid should unique , enough determine equality. overridden methods this:

    @override public int hashcode() {     final int prime = 31;     int result = 1;     result = prime * result + ((muid == null) ? 0 : muid.hashcode());     return result; }  @override public boolean equals(object obj) {     if (this == obj)         return true;     if (obj == null)         return false;     if (getclass() != obj.getclass())         return false;     datasource other = (datasource) obj;     if (muid == null) {         if (other.muid != null)             return false;     } else if (!muid.equals(other.muid))         return false;     return true; } 

but reason, when compare 2 objects of class, returns false. i've stepped through code debugger , can determine muid strings same. equals(object) method returns false @ line

if (!muid.equals(other.muid)) {return false;}.

this means string equals() method saying strings not equal. but, in debugger can see hashcodes equal.

here debugging screenshot: debugging screenshot can please explain happening? thanks.

update: works!

i should point out not calling equals(...) method directly, rather list<datasource> list.contains(datasource). turns out if step through equals(object) code, shows me returns false comparing every object in list (even if there match). seems list.contains() method returns right value (true or false) anyway. don't know why is, works. had semicolon after if(list.contains(datasource)); not rightly see contains() working fine. thanks everyone.

your method should work. tested code , worked fine:

public class datasource {      private string muid;      public datasource(string m) {         muid = m;     }      @override     public int hashcode() {         final int prime = 31;         int result = 1;         result = prime * result + ((muid == null) ? 0 : muid.hashcode());         return result;     }      @override     public boolean equals(object obj) {         if (this == obj)             return true;         if (obj == null)             return false;         if (getclass() != obj.getclass())             return false;         datasource other = (datasource) obj;         if (muid == null) {             if (other.muid != null)                 return false;         } else if (!muid.equals(other.muid))             return false;         return true;     }      /**      * @param args      */     public static void main(string[] args) {         // todo auto-generated method stub         datasource ds1 = new datasource("test");         datasource ds2 = new datasource("test");         system.out.println(ds1.equals(ds2));     }  } 

the console output "true"; when changed code datasource ds2 = new datasource("test1"); returned "false";


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -