Making enum hashcodes consistent across JVM's

dfranken.jdk at gmail.com dfranken.jdk at gmail.com
Thu Mar 17 12:49:08 UTC 2022


Dear all,

Currently enums do not have a well-defined hashCode() implementation so
they defer to Object.hashCode() which just uses an internal mechanism
to determine the hashcode, likely based on the object's place in the
heap.

This may confuse a lot of developers as other classes such as String do
have a well-defined hashcode which is consistent across multiple JVM's.

Could it be a good idea to make enums' hashCode() method more reliably
and predictable? For instance by using the hashcode of the name of its
value?

For example:

class MyEnum { A, B }
-> A.hashCode() == A.name().hashCode()

Kind regards,

Dave Franken


More information about the core-libs-dev mailing list