Hi I am trying to overriding the equal and hashcode method in for my custom class .
here is my custom class
public class property
{
public String type = null;
public int value;
public int id;
public String name = null;
public String course = null;
public property(String type, String course, int value, int id)
{
this.value = value;
this.type = type;
this.course = course;
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
property sig = (property) obj;
if(sig != null) {
if(sig.type.equals("EST1")) {
if (sig.name != null && sig.type != null && sig.course != null) {
Log.d("property", "Other " + sig.course + "AHHAHAHA " + course + "shahahaaha " + sig.id + "babababaab " + id);
if (sig.course.equals(this.course) && sig.value == this.value && sig.type.equals(this.type) && sig.id == (this.id)) {
Log.d("property", "EST1");
return true;
}
}
}
return false;
}
The problem is that "this.id, this.type" is always null or 0 I do not understand why...while sig.id, sig.type have values. Just to let you know it is only code snippet, not full code so it might have typos and not logically correct..
Your code works well in my side, so I guess your test program is somewhat different (or complicated?) from mine.
I tried following for test,
property p = new property("a","b", 100, 200);
System.out.println("id:"+p.id);
System.out.println("type:"+p.type);
System.out.println(p.equals(p) +" should be true..");
And I got,
id:200
type:a
true should be true..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With