How is the hashcode computed for a card object which consists of enum suit and enum rank ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re using Eclipse, it can generate a “good enough”
hashCode()implementation for you:I can only imagine that NetBeans, IntelliJ IDEA, etc., can do this as well.
That said, since the domain is small, this implementation will work equally well (I think…):
This assumes that
Rankordinals are0-12inclusive andSuitordinals are0-3inclusive. Note that most of the ugliness there comes from the null checks. If the values can never be null, then: