Consider the following snippet:
import java.util.*;
public class EqualsOverload {
public static void main(String[] args) {
class Thing {
Let me share an example of "buggy code" with Overloaded equals:
class A{
private int val;
public A(int i){
this.val = i;
}
public boolean equals(A a){
return a.val == this.val;
}
@Override
public int hashCode() {
return Objects.hashCode(this.val);
}
}
public class TestOverloadEquals {
public static void main(String[] args){
A a1 = new A(1), a2 = new A(2);
List list = new ArrayList<>();
list.add(a1);
list.add(a2);
A a3 = new A(1);
System.out.println(list.contains(a3));
}
}