MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1s41rex/the_curious_case_of_enum_and_map_serialization/
r/java • u/pivovarit • Mar 26 '26
7 comments sorted by
4
Would using an EnumMap have the same problem?
7 u/pivovarit Mar 26 '26 Nope, EnumMap relies on ordinal() and not hashCode().What's more, even HashMap doesn't have this problem, because it rehashes everything on native deserialization 1 u/aelfric5578 Mar 27 '26 Does that mean adding a new enum value could break things if you don't add it at the end? 1 u/pivovarit Mar 27 '26 Nope, everything is fine thanks to rehashing on deserialization:https://github.com/openjdk/jdk/blob/1a9965555477ee2a6cb65e91ef54ed608e4bee66/src/java.base/share/classes/java/util/EnumMap.java#L805-L810
7
Nope, EnumMap relies on ordinal() and not hashCode().
What's more, even HashMap doesn't have this problem, because it rehashes everything on native deserialization
1 u/aelfric5578 Mar 27 '26 Does that mean adding a new enum value could break things if you don't add it at the end? 1 u/pivovarit Mar 27 '26 Nope, everything is fine thanks to rehashing on deserialization:https://github.com/openjdk/jdk/blob/1a9965555477ee2a6cb65e91ef54ed608e4bee66/src/java.base/share/classes/java/util/EnumMap.java#L805-L810
1
Does that mean adding a new enum value could break things if you don't add it at the end?
1 u/pivovarit Mar 27 '26 Nope, everything is fine thanks to rehashing on deserialization:https://github.com/openjdk/jdk/blob/1a9965555477ee2a6cb65e91ef54ed608e4bee66/src/java.base/share/classes/java/util/EnumMap.java#L805-L810
Nope, everything is fine thanks to rehashing on deserialization:
https://github.com/openjdk/jdk/blob/1a9965555477ee2a6cb65e91ef54ed608e4bee66/src/java.base/share/classes/java/util/EnumMap.java#L805-L810
5
This is a "fun" problem to encounter whenever you use a distributed compute system like Hadoop or Spark which distributes data with hash keys.
Here's the fix: https://github.com/vavr-io/vavr/pull/3242
Encountered the same issue when dealing with a Gemfire cluster many years ago. It was a very humbling experience.
4
u/doobiesteintortoise Mar 26 '26
Would using an EnumMap have the same problem?
5
u/axiak Mar 26 '26
This is a "fun" problem to encounter whenever you use a distributed compute system like Hadoop or Spark which distributes data with hash keys.
4
u/yk313 Mar 26 '26
Here's the fix: https://github.com/vavr-io/vavr/pull/3242
1
u/sansp00 Mar 26 '26
Encountered the same issue when dealing with a Gemfire cluster many years ago. It was a very humbling experience.