here’s some code that might demonstrate how it works out:
import java.util.ArrayList;
class A {
private Long id;
private String name;
A(Long id){
this.id = id;
}
@Override
public boolean equals(Object v) {
boolean retVal = false;
if (v instanceof A){
A ptr = (A) v;
retVal = ptr.id.longValue() == this.id;
}
return retVal;
}
@Override
public int hashCode() {
int hash = 7;
hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
}
public class ArrayList_recap {
public static void main(String[] args) {
ArrayList<A> list = new ArrayList<A>();
list.add(new A(0L));
list.add(new A(1L));
if (list.contains(new A(0L)))
{
System.out.println("Equal");
}
else
{
System.out.println("Nah.");
}
}
}
First, there is an override of the equals(Object o) method. Then there is the override of the hashCode() as well. Also note that the instanceof A check in the equals will ensure that you’re not trying to compare different objects.
That should do the trick! Hope it helped! Cheers 🙂