I have this class:
public class Person {
private String id;
@Override
public boolean equals(Object obj) {
return obj instanceof Person &&
this.id.equals(((Person) obj).id);
}
@Override
public int hashCode() {
...
}
}
The field id
consists solely of digits. My question is, should the hashCode
method look like this:
@Override
public int hashCode() {
return id.hashCode();
}
or this:
@Override
public int hashCode() {
return Integer.parseInt(id);
}
Provided that id <= Integer.MAX_VALUE
.