Skip to content

Commit 874a296

Browse files
dreis2211snicoll
authored andcommitted
Avoid resizing of Maps created by CollectionUtils
See gh-29190
1 parent d872e7c commit 874a296

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,7 +85,8 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) {
8585
* @see #newLinkedHashMap(int)
8686
*/
8787
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
88-
return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
88+
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
89+
return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR);
8990
}
9091

9192
/**
@@ -102,7 +103,8 @@ public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
102103
* @see #newHashMap(int)
103104
*/
104105
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
105-
return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
106+
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
107+
return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR);
106108
}
107109

108110
/**

0 commit comments

Comments
 (0)