In relation to this question (Efficient hashCode() implementation) I have one more question.
I have a "value" class which instances are persisted in a database. Thus this class's instances all have a unique ID.
As a consequence I implemented the hash code method (and related equals method) simply by returning this id.
When using Eclipse hashcode generator and telling Eclipse to use only the ID attribute for generation I have the following method:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
I think simply returning id is more efficient since I KNOW this id is unique. Am I right ?
Thanks in advance