diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 3658223bf773..79e9800a9e03 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -85,8 +85,7 @@ public static boolean isEmpty(@Nullable Map map) { * @see #newLinkedHashMap(int) */ public static HashMap newHashMap(int expectedSize) { - int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); - return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR); + return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); } /** @@ -103,8 +102,11 @@ public static HashMap newHashMap(int expectedSize) { * @see #newHashMap(int) */ public static LinkedHashMap newLinkedHashMap(int expectedSize) { - int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); - return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR); + return new LinkedHashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + } + + private static int computeMapInitialCapacity(int expectedSize) { + return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); } /**