Browse Source

Avoid resizing of Maps created by CollectionUtils

See gh-29190
pull/30296/head
Christoph Dreis 4 years ago committed by Stephane Nicoll
parent
commit
874a296a76
  1. 8
      spring-core/src/main/java/org/springframework/util/CollectionUtils.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -85,7 +85,8 @@ public abstract class CollectionUtils { @@ -85,7 +85,8 @@ public abstract class CollectionUtils {
* @see #newLinkedHashMap(int)
*/
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR);
}
/**
@ -102,7 +103,8 @@ public abstract class CollectionUtils { @@ -102,7 +103,8 @@ public abstract class CollectionUtils {
* @see #newHashMap(int)
*/
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR);
}
/**

Loading…
Cancel
Save