Polish "Avoid resizing of Maps created by CollectionUtils"

See gh-29190
This commit is contained in:
Stephane Nicoll
2022-09-26 09:00:10 +02:00
parent 874a296a76
commit 7309fe9f2e

View File

@@ -85,8 +85,7 @@ public abstract class CollectionUtils {
* @see #newLinkedHashMap(int)
*/
public static <K, V> HashMap<K, V> 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 abstract class CollectionUtils {
* @see #newHashMap(int)
*/
public static <K, V> LinkedHashMap<K, V> 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);
}
/**