Browse Source

Polish "Avoid resizing of Maps created by CollectionUtils"

See gh-29190
pull/30296/head
Stephane Nicoll 3 years ago
parent
commit
7309fe9f2e
  1. 10
      spring-core/src/main/java/org/springframework/util/CollectionUtils.java

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

@ -85,8 +85,7 @@ public abstract class CollectionUtils { @@ -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 { @@ -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);
}
/**

Loading…
Cancel
Save