Skip to content

Commit cdc4497

Browse files
committed
Restore creation of plain HashSet/HashMap for direct HashSet/HashMap type
Closes gh-30596
1 parent 6cc084d commit cdc4497

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
@@ -179,7 +179,7 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
179179
@SuppressWarnings("unchecked")
180180
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {
181181
Assert.notNull(collectionType, "Collection type must not be null");
182-
if (LinkedHashSet.class == collectionType || HashSet.class == collectionType ||
182+
if (LinkedHashSet.class == collectionType ||
183183
Set.class == collectionType || Collection.class == collectionType) {
184184
return new LinkedHashSet<>(capacity);
185185
}
@@ -197,6 +197,9 @@ else if (EnumSet.class.isAssignableFrom(collectionType)) {
197197
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
198198
return EnumSet.noneOf(asEnumType(elementType));
199199
}
200+
else if (HashSet.class == collectionType) {
201+
return new HashSet<>(capacity);
202+
}
200203
else {
201204
if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) {
202205
throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
@@ -297,7 +300,7 @@ public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) {
297300
@SuppressWarnings({"rawtypes", "unchecked"})
298301
public static <K, V> Map<K, V> createMap(Class<?> mapType, @Nullable Class<?> keyType, int capacity) {
299302
Assert.notNull(mapType, "Map type must not be null");
300-
if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType) {
303+
if (LinkedHashMap.class == mapType || Map.class == mapType) {
301304
return new LinkedHashMap<>(capacity);
302305
}
303306
else if (LinkedMultiValueMap.class == mapType || MultiValueMap.class == mapType) {
@@ -310,6 +313,9 @@ else if (EnumMap.class == mapType) {
310313
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
311314
return new EnumMap(asEnumType(keyType));
312315
}
316+
else if (HashMap.class == mapType) {
317+
return new HashMap<>(capacity);
318+
}
313319
else {
314320
if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) {
315321
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
@@ -217,15 +217,15 @@ void createsCollectionsCorrectly() {
217217

218218
// concrete types
219219
testCollection(ArrayList.class, ArrayList.class);
220-
testCollection(HashSet.class, LinkedHashSet.class);
220+
testCollection(HashSet.class, HashSet.class);
221221
testCollection(LinkedHashSet.class, LinkedHashSet.class);
222222
testCollection(TreeSet.class, TreeSet.class);
223223
}
224224

225225
private void testCollection(Class<?> collectionType, Class<?> resultType) {
226226
assertThat(CollectionFactory.isApproximableCollectionType(collectionType)).isTrue();
227-
assertThat(createCollection(collectionType, 0)).isInstanceOf(resultType);
228-
assertThat(createCollection(collectionType, String.class, 0)).isInstanceOf(resultType);
227+
assertThat(createCollection(collectionType, 0)).isExactlyInstanceOf(resultType);
228+
assertThat(createCollection(collectionType, String.class, 0)).isExactlyInstanceOf(resultType);
229229
}
230230

231231
@Test
@@ -266,16 +266,16 @@ void createsMapsCorrectly() {
266266
testMap(MultiValueMap.class, LinkedMultiValueMap.class);
267267

268268
// concrete types
269-
testMap(HashMap.class, LinkedHashMap.class);
269+
testMap(HashMap.class, HashMap.class);
270270
testMap(LinkedHashMap.class, LinkedHashMap.class);
271271
testMap(TreeMap.class, TreeMap.class);
272272
testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class);
273273
}
274274

275275
private void testMap(Class<?> mapType, Class<?> resultType) {
276276
assertThat(CollectionFactory.isApproximableMapType(mapType)).isTrue();
277-
assertThat(createMap(mapType, 0)).isInstanceOf(resultType);
278-
assertThat(createMap(mapType, String.class, 0)).isInstanceOf(resultType);
277+
assertThat(createMap(mapType, 0)).isExactlyInstanceOf(resultType);
278+
assertThat(createMap(mapType, String.class, 0)).isExactlyInstanceOf(resultType);
279279
}
280280

281281
@Test

0 commit comments

Comments
 (0)