Skip to content

Commit 433a23a

Browse files
committed
Merge pull request #29190 from dreis2211
* pr/29190: Polish "Avoid resizing of Maps created by CollectionUtils" Avoid resizing of Maps created by CollectionUtils Closes gh-29190
2 parents d872e7c + 7309fe9 commit 433a23a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 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,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-
return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
88+
return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
8989
}
9090

9191
/**
@@ -102,7 +102,11 @@ public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
102102
* @see #newHashMap(int)
103103
*/
104104
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
105-
return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), 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);
106110
}
107111

108112
/**

0 commit comments

Comments
 (0)