Junior
On which fields should you make hashCode if equals is sufficient?
sobes.tech AI
Answer from AI
If the equals method is already defined and satisfactory, then hashCode should be based on the same fields that participate in the equals comparison. This is necessary to maintain the contract between equals and hashCode: objects that are equal according to equals must have the same hashCode.
For example, if equals compares the fields id and name, then hashCode should be computed based on these same fields:
@Override
public int hashCode() {
return Objects.hash(id, name);
}