Answer the question
In order to leave comments, you need to log in
What does the number 31 mean in the hashCode function definition?
I often see it in code.
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((department == null) ? 0 : department.hashCode());
result = prime * result + ((fax == null) ? 0 : fax.hashCode());
result = prime * result + (female ? 1231 : 1237);
result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode());
result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode());
result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode());
result = prime * result + (male ? 1231 : 1237);
result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phones == null) ? 0 : phones.hashCode());
return result;
}
public int hashCode() {
int result =
Objects.hash(
firstName,
lastName,
age,
nationalities,
weight,
tattoos,
hasChildren,
superWildcard,
extendsWildcard,
unboundedWildcard,
objectList,
foodPreferences,
setListMapArrayInt);
result = 31 * result + Arrays.hashCode(favoriteThreeNumbers);
result = 31 * result + Arrays.hashCode(favoriteArrayValues);
result = 31 * result + Arrays.hashCode(nestedArray);
return result;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question