Skip to content

Commit 53f8912

Browse files
committed
Merge branch '6.0.x'
# Conflicts: # spring-core/src/main/java/org/springframework/core/CollectionFactory.java
2 parents 4fb4c95 + cdc4497 commit 53f8912

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
181181
@SuppressWarnings("unchecked")
182182
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {
183183
Assert.notNull(collectionType, "Collection type must not be null");
184-
if (LinkedHashSet.class == collectionType || HashSet.class == collectionType ||
184+
if (LinkedHashSet.class == collectionType ||
185185
Set.class == collectionType || Collection.class == collectionType ||
186186
collectionType.getName().equals("java.util.SequencedSet") ||
187187
collectionType.getName().equals("java.util.SequencedCollection")) {
@@ -201,6 +201,9 @@ else if (EnumSet.class.isAssignableFrom(collectionType)) {
201201
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
202202
return EnumSet.noneOf(asEnumType(elementType));
203203
}
204+
else if (HashSet.class == collectionType) {
205+
return new HashSet<>(capacity);
206+
}
204207
else {
205208
if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) {
206209
throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
@@ -302,7 +305,7 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
302305
@SuppressWarnings({"rawtypes", "unchecked"})
303306
public static <K, V> Map<K, V> createMap(Class<?> mapType, @Nullable Class<?> keyType, int capacity) {
304307
Assert.notNull(mapType, "Map type must not be null");
305-
if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType ||
308+
if (LinkedHashMap.class == mapType || Map.class == mapType ||
306309
mapType.getName().equals("java.util.SequencedMap")) {
307310
return new LinkedHashMap<>(capacity);
308311
}
@@ -316,6 +319,9 @@ else if (EnumMap.class == mapType) {
316319
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
317320
return new EnumMap(asEnumType(keyType));
318321
}
322+
else if (HashMap.class == mapType) {
323+
return new HashMap<>(capacity);
324+
}
319325
else {
320326
if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) {
321327
throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName());

spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ void createsCollectionsCorrectly() {
219219

220220
// concrete types
221221
testCollection(ArrayList.class, ArrayList.class);
222-
testCollection(HashSet.class, LinkedHashSet.class);
222+
testCollection(HashSet.class, HashSet.class);
223223
testCollection(LinkedHashSet.class, LinkedHashSet.class);
224224
testCollection(TreeSet.class, TreeSet.class);
225225
}
226226

227227
private void testCollection(Class<?> collectionType, Class<?> resultType) {
228228
assertThat(CollectionFactory.isApproximableCollectionType(collectionType)).isTrue();
229-
assertThat(createCollection(collectionType, 0)).isInstanceOf(resultType);
230-
assertThat(createCollection(collectionType, String.class, 0)).isInstanceOf(resultType);
229+
assertThat(createCollection(collectionType, 0)).isExactlyInstanceOf(resultType);
230+
assertThat(createCollection(collectionType, String.class, 0)).isExactlyInstanceOf(resultType);
231231
}
232232

233233
@Test
@@ -269,16 +269,16 @@ void createsMapsCorrectly() {
269269
testMap(MultiValueMap.class, LinkedMultiValueMap.class);
270270

271271
// concrete types
272-
testMap(HashMap.class, LinkedHashMap.class);
272+
testMap(HashMap.class, HashMap.class);
273273
testMap(LinkedHashMap.class, LinkedHashMap.class);
274274
testMap(TreeMap.class, TreeMap.class);
275275
testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class);
276276
}
277277

278278
private void testMap(Class<?> mapType, Class<?> resultType) {
279279
assertThat(CollectionFactory.isApproximableMapType(mapType)).isTrue();
280-
assertThat(createMap(mapType, 0)).isInstanceOf(resultType);
281-
assertThat(createMap(mapType, String.class, 0)).isInstanceOf(resultType);
280+
assertThat(createMap(mapType, 0)).isExactlyInstanceOf(resultType);
281+
assertThat(createMap(mapType, String.class, 0)).isExactlyInstanceOf(resultType);
282282
}
283283

284284
@Test

0 commit comments

Comments
 (0)