I am running out of ideas... I'm comparing two objects, they both have an array list field of custom type.
Both objects contain one element in the array list.
When I look into eclipse debugger it looks entirely the same, down to closest detail, except for the different id ( it looks like : (id=111) in the debugger)
The funny thing is, this differing ID is on one field which is of type Integer (counter). Which obviously should not happen as integer has equals and hashcode implemented already, right?
And the other field with differing ID is of type string (filter)
The equals method between two objects having these fields is returning false... I'm running out of ideas why... All equals and hashCode methods are implemented in every custom type..
This is from eclipse debugger:
Object 1:
Object (id=159)
arrayList ArrayList<E> (id=175)
[0] Item (id=175)
counter Integer (id=179)
filter "abcd" (id=181)
count 4
hash -717152022
offset 2
value (id=189)
Object 2:
Object (id=259)
arrayList ArrayList<E> (id=267)
[0] Item (id=268)
counter Integer (id=268)
filter "abcd" (id=269)
count 4
hash -717152022
offset 2
value (id=270)
You can simply use the equals() implementation of ArrayList super class : AbstractList.
If and only if you have overridden the equals() method in your class Item.
For example :
@Override
public boolean equals(Item item){
if(this.counter.equals(item.counter)
&& this.filter.equals(item.filter)){
// && etc for all Item fields that make the equality
return true;
}
return false;
}
Also note that you can't rely on hashCode to compare equality, see this post : Is Java hashCode() method a reliable measure of object equality?
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