There is an excellent discussion on the correct implementation of equals and hashCode here :
Whenever a.equals (b), then a.hashCode () should be the same as b.hashCode ()
This is the only rule that matters. There is no proper implementation of hashCode beyond this one rule. There are better and worse hash codes in terms of performance and hash collisions, but that's a completely different topic.
Your code looks correct according to this rule, because if a.equals(b) , then firstname.hashCode()+lastname.hashCode() should be the same value for a and b .
source share