Skip to content

Commit 7309fe9

Browse files
committed
Polish "Avoid resizing of Maps created by CollectionUtils"
See gh-29190
1 parent 874a296 commit 7309fe9

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

spring-core/src/main/java/org/springframework/util/CollectionUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) {
8585
* @see #newLinkedHashMap(int)
8686
*/
8787
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
88-
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
89-
return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR);
88+
return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
9089
}
9190

9291
/**
@@ -103,8 +102,11 @@ public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
103102
* @see #newHashMap(int)
104103
*/
105104
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
106-
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
107-
return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR);
105+
return new LinkedHashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
106+
}
107+
108+
private static int computeMapInitialCapacity(int expectedSize) {
109+
return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
108110
}
109111

110112
/**

0 commit comments

Comments
 (0)