Skip to content

Commit 2b47e77

Browse files
committed
Explicit explanation that no resize/rehash operations will be needed
See gh-25349
1 parent ff11467 commit 2b47e77

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,36 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) {
7373

7474
/**
7575
* Instantiate a new {@link HashMap} with an initial capacity
76-
* that can accommodate the given number of elements.
76+
* that can accommodate the specified number of elements without
77+
* any immediate resize/rehash operations to be expected.
7778
* <p>This differs from the regular {@link HashMap} constructor
7879
* which takes an initial capacity relative to a load factor
7980
* but is effectively aligned with the JDK's
8081
* {@link java.util.concurrent.ConcurrentHashMap#ConcurrentHashMap(int)}.
81-
* @param expectedSize the expected number of elements
82+
* @param expectedSize the expected number of elements (with a corresponding
83+
* capacity to be derived so that no resize/rehash operations are needed)
8284
* @since 5.3
8385
* @see #newLinkedHashMap(int)
8486
*/
85-
@SuppressWarnings({"rawtypes", "unchecked"})
8687
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
87-
return new HashMap((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
88+
return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
8889
}
8990

9091
/**
9192
* Instantiate a new {@link LinkedHashMap} with an initial capacity
92-
* that can accommodate the given number of elements.
93+
* that can accommodate the specified number of elements without
94+
* any immediate resize/rehash operations to be expected.
9395
* <p>This differs from the regular {@link LinkedHashMap} constructor
9496
* which takes an initial capacity relative to a load factor but is
9597
* aligned with Spring's own {@link LinkedCaseInsensitiveMap} and
9698
* {@link LinkedMultiValueMap} constructor semantics as of 5.3.
97-
* @param expectedSize the expected number of elements
99+
* @param expectedSize the expected number of elements (with a corresponding
100+
* capacity to be derived so that no resize/rehash operations are needed)
98101
* @since 5.3
102+
* @see #newHashMap(int)
99103
*/
100-
@SuppressWarnings({"rawtypes", "unchecked"})
101104
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
102-
return new LinkedHashMap((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
105+
return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
103106
}
104107

105108
/**

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ public LinkedCaseInsensitiveMap() {
8181
* @see #convertKey(String)
8282
*/
8383
public LinkedCaseInsensitiveMap(@Nullable Locale locale) {
84-
this(16, locale);
84+
this(12, locale); // equivalent to LinkedHashMap's initial capacity of 16
8585
}
8686

8787
/**
8888
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
89-
* with an initial capacity that can accommodate the given number of elements,
89+
* with an initial capacity that can accommodate the specified number of
90+
* elements without any immediate resize/rehash operations to be expected,
9091
* storing case-insensitive keys according to the default Locale (in lower case).
91-
* @param expectedSize the expected number of elements
92+
* @param expectedSize the expected number of elements (with a corresponding
93+
* capacity to be derived so that no resize/rehash operations are needed)
94+
* @see CollectionUtils#newHashMap(int)
9295
* @see #convertKey(String)
9396
*/
9497
public LinkedCaseInsensitiveMap(int expectedSize) {
@@ -97,10 +100,13 @@ public LinkedCaseInsensitiveMap(int expectedSize) {
97100

98101
/**
99102
* Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
100-
* with an initial capacity that can accommodate the given number of elements,
103+
* with an initial capacity that can accommodate the specified number of
104+
* elements without any immediate resize/rehash operations to be expected,
101105
* storing case-insensitive keys according to the given Locale (in lower case).
102-
* @param expectedSize the expected number of elements
106+
* @param expectedSize the expected number of elements (with a corresponding
107+
* capacity to be derived so that no resize/rehash operations are needed)
103108
* @param locale the Locale to use for case-insensitive key conversion
109+
* @see CollectionUtils#newHashMap(int)
104110
* @see #convertKey(String)
105111
*/
106112
public LinkedCaseInsensitiveMap(int expectedSize, @Nullable Locale locale) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ public LinkedMultiValueMap() {
4949

5050
/**
5151
* Create a new LinkedMultiValueMap that wraps a {@link LinkedHashMap}
52-
* with an initial capacity that can accommodate the given number of elements.
53-
* @param expectedSize the expected number of elements
52+
* with an initial capacity that can accommodate the specified number of
53+
* elements without any immediate resize/rehash operations to be expected.
54+
* @param expectedSize the expected number of elements (with a corresponding
55+
* capacity to be derived so that no resize/rehash operations are needed)
56+
* @see CollectionUtils#newLinkedHashMap(int)
5457
*/
5558
public LinkedMultiValueMap(int expectedSize) {
5659
super(CollectionUtils.newLinkedHashMap(expectedSize));

0 commit comments

Comments
 (0)