You should override the equals()
and hashcode()
method in your MyKey class (or any other custom class for that matter that would go into a HashMap or Set). That way, when you say containsKey()
(or the Set methodcontains()
, or equals()
), you would have explicitly specified what properties of MyKey to check for containment or equality.
You could have a simple equals()
and hashCode()
method like this in your MyKey class
@Override
public boolean equals(Object o) {
boolean result = false;
if (o instanceof MyKey) {
MyKey mykey = (MyKey) o;
result = (this.getX() == mykey.getX() && mykey.getY() == mykey.getY());
}
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + X;
result = prime * result + Y;
return result;
}
(Assuming the fields in your MyKey class are X and Y)