Seetharaman Venkatasamy wrote:then change your equals and hashCode implementation to
. . .
That equals method won't work; it will be prone to Exceptions because of the cast. You need to
test for the type of the obj parameter before using it. I would suggest you write it like this.
This will still suffer problems if you allow
null values for
age and
name. Then you would be better off like this:
... ((Key)obj).age == null ? this.age == null : ((Key)obj).age.equals(this.age) && ...
Obviously you can use as many fields or as few as you wish in the equals method, but yo umust use the same fields in the hashCode method.