Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:39:42+00:00 2026-05-22T14:39:42+00:00

How is the hashcode computed for a card object which consists of enum suit

  • 0

How is the hashcode computed for a card object which consists of enum suit and enum rank ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:39:43+00:00Added an answer on May 22, 2026 at 2:39 pm

    If you’re using Eclipse, it can generate a “good enough” hashCode() implementation for you:

    context menu screenshot

    public class Card
    {
        private Suit suit;
        private Rank rank;
    
        @Override
        public int hashCode()
        {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((rank == null) ? 0 : rank.hashCode());
            result = prime * result + ((suit == null) ? 0 : suit.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj)
        {
            if (this == obj) return true;
            if (!(obj instanceof Card)) return false;
            Card other = (Card) obj;
            if (rank != other.rank) return false;
            if (suit != other.suit) return false;
            return true;
        }
    }
    

    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…):

    public int hashCode()
    {
        int rankHash = ((rank == null) ? 0 : (1+rank.ordinal()));
        int suitHash = ((suit == null) ? 0 : (1+suit.ordinal()));
        return rankHash + 31*suitHash;
    }
    

    This assumes that Rank ordinals are 0-12 inclusive and Suit ordinals are 0-3 inclusive. Note that most of the ugliness there comes from the null checks. If the values can never be null, then:

    public int hashCode()
    {
        return rank.ordinal() + 31*suit.ordinal();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Per the Java documentation, the hash code for a String object is computed as:
The JDK documentation for java.lang.String.hashCode() famously says: The hash code for a String object
I have a class Foo which overrides equals() and hashCode() properly. I would like
If I call the Object.hashcode() method on some object it returns the internal address
In our application we generate hashcode from a java object and store it in
The hashCode value of a Java String is computed as ( String.hashCode() ): s[0]*31^(n-1)
Typically the default implementation of Object.hashCode() is some function of the allocated address of
When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the
The Javadocs say: Returns a hashcode for this Method. The hashcode is computed as
Why does the Hashcode of an Object change in Java? Does it change at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.