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

  • SEARCH
  • Home
  • 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 3934960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:46:32+00:00 2026-05-19T23:46:32+00:00

The method hashCode() in class Enum is final and defined as super.hashCode(), which means

  • 0

The method hashCode() in class Enum is final and defined as super.hashCode(), which means it returns a number based on the address of the instance, which is a random number from programmers POV.

Defining it e.g. as ordinal() ^ getClass().getName().hashCode() would be deterministic across different JVMs. It would even work a bit better, since the least significant bits would “change as much as possible”, e.g., for an enum containing up to 16 elements and a HashMap of size 16, there’d be for sure no collisions (sure, using an EnumMap is better, but sometimes not possible, e.g. there’s no ConcurrentEnumMap). With the current definition you have no such guarantee, have you?

Summary of the answers

Using Object.hashCode() compares to a nicer hashCode like the one above as follows:

  • PROS
    • simplicity
  • CONTRAS
    • speed
    • more collisions (for any size of a HashMap)
    • non-determinism, which propagates to other objects making them unusable for
      • deterministic simulations
      • ETag computation
      • hunting down bugs depending e.g. on a HashSet iteration order

I’d personally prefer the nicer hashCode, but IMHO no reason weights much, maybe except for the speed.

UPDATE

I was curious about the speed and wrote a benchmark with surprising results. For a price of a single field per class you can a deterministic hash code which is nearly four times faster. Storing the hash code in each field would be even faster, although negligibly.

The explanation why the standard hash code is not much faster is that it can’t be the object’s address as objects gets moved by the GC.

UPDATE 2

There are some strange things going on with the hashCode performance in general. When I understand them, there’s still the open question, why System.identityHashCode (reading from the object header) is way slower than accessing a normal object field.

  • 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-19T23:46:32+00:00Added an answer on May 19, 2026 at 11:46 pm

    I think that the reason they made it final is to avoid developers shooting themselves in the foot by rewriting a suboptimal (or even incorrect) hashCode.

    Regarding the chosen implementation: it’s not stable across JVMs, but it’s very fast, avoid collisions, and doesn’t need an additional field in the enum. Given the normally small number of instances of an enum class, and the speed of the equals method, I wouldn’t be surprised if the HashMap lookup time was bigger with your algorithm than with the current one, due to its additional complexity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

equals and hashCode method must be consistent, which means that when two objects are
I understand that the String class' hashCode() method is not guarantied to generate unique
I often auto-generate an class's hashCode() method using IntelliJ IDEA and typically the method
When a class in Java doesn't override hashCode() , printing an instance of this
The hashCode() method of class Boolean is implemented like this: public int hashCode() {
The Javadocs say: Returns a hashcode for this Method. The hashcode is computed as
What method do I call to get the name of a class?
Which method is preferred? Session.Remove(foo); Session[foo] = null; Is there a difference?
Which method would you recommend to pass data from a child view back to
Which method makes the most sense for importing a module in python that is

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.