@@ -50,9 +50,16 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
5050 }
5151 };
5252
53- private final Map <String , T > nameToConstant = new HashMap <>();
54- private final Map <String , T > stringToConstant = new HashMap <>();
55- private final Map <T , String > constantToName = new HashMap <>();
53+ /**
54+ * Taken from Java 19 method {@link HashMap.newHashMap}, using default load factor {@code 0.75F}.
55+ */
56+ private static int calculateHashMapCapacity (int numMappings ) {
57+ return (int ) Math .ceil (numMappings / 0.75F );
58+ }
59+
60+ private final Map <String , T > nameToConstant ;
61+ private final Map <String , T > stringToConstant ;
62+ private final Map <T , String > constantToName ;
5663
5764 private EnumTypeAdapter (Class <T > classOfT ) {
5865 try {
@@ -71,6 +78,12 @@ private EnumTypeAdapter(Class<T> classOfT) {
7178 // one declared field which is not an enum constant, namely the implicit $VALUES array
7279 fields = Arrays .copyOf (fields , constantCount );
7380
81+ int hashMapCapacity = calculateHashMapCapacity (constantCount );
82+ nameToConstant = new HashMap <>(hashMapCapacity );
83+ stringToConstant = new HashMap <>(hashMapCapacity );
84+ // Don't use `EnumMap` here; that can break when using ProGuard or R8
85+ constantToName = new HashMap <>(hashMapCapacity );
86+
7487 AccessibleObject .setAccessible (fields , true );
7588
7689 for (Field constantField : fields ) {
0 commit comments