diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index c52bddcc6232..56088ab099a1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -89,6 +89,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -164,7 +165,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA protected final Log logger = LogFactory.getLog(getClass()); - private final Set> autowiredAnnotationTypes = new LinkedHashSet<>(4); + private final Set> autowiredAnnotationTypes = CollectionUtils.newLinkedHashSet(4); private String requiredParameterName = "required"; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java index 708064488acf..a9efca3c555a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java @@ -363,7 +363,7 @@ public LifecycleMetadata(Class beanClass, Collection initMet } public void checkInitDestroyMethods(RootBeanDefinition beanDefinition) { - Set checkedInitMethods = new LinkedHashSet<>(this.initMethods.size()); + Set checkedInitMethods = CollectionUtils.newLinkedHashSet(this.initMethods.size()); for (LifecycleMethod lifecycleMethod : this.initMethods) { String methodIdentifier = lifecycleMethod.getIdentifier(); if (!beanDefinition.isExternallyManagedInitMethod(methodIdentifier)) { @@ -374,7 +374,7 @@ public void checkInitDestroyMethods(RootBeanDefinition beanDefinition) { } } } - Set checkedDestroyMethods = new LinkedHashSet<>(this.destroyMethods.size()); + Set checkedDestroyMethods = CollectionUtils.newLinkedHashSet(this.destroyMethods.size()); for (LifecycleMethod lifecycleMethod : this.destroyMethods) { String methodIdentifier = lifecycleMethod.getIdentifier(); if (!beanDefinition.isExternallyManagedDestroyMethod(methodIdentifier)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java index d1a5946aa0fb..c31616313f1f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java @@ -23,13 +23,13 @@ import java.lang.reflect.Method; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.Set; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ReflectionUtils; /** @@ -124,7 +124,7 @@ public void checkConfigMembers(RootBeanDefinition beanDefinition) { this.checkedElements = Collections.emptySet(); } else { - Set checkedElements = new LinkedHashSet<>((this.injectedElements.size() * 4 / 3) + 1); + Set checkedElements = CollectionUtils.newLinkedHashSet(this.injectedElements.size()); for (InjectedElement element : this.injectedElements) { Member member = element.getMember(); if (!beanDefinition.isExternallyManagedConfigMember(member)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java index 2f0b6bc2a09c..35a3995c3e53 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java @@ -19,7 +19,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; -import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -40,6 +39,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; /** @@ -59,7 +59,7 @@ */ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwareAutowireCandidateResolver { - private final Set> qualifierTypes = new LinkedHashSet<>(2); + private final Set> qualifierTypes = CollectionUtils.newLinkedHashSet(2); private Class valueAnnotationType = Value.class; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredMethodArgumentsResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredMethodArgumentsResolver.java index 9c79f232114a..cca47efb7670 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredMethodArgumentsResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredMethodArgumentsResolver.java @@ -18,7 +18,6 @@ import java.lang.reflect.Method; import java.util.Arrays; -import java.util.LinkedHashSet; import java.util.Set; import java.util.stream.Collectors; @@ -34,6 +33,7 @@ import org.springframework.core.MethodParameter; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.function.ThrowingConsumer; @@ -165,7 +165,7 @@ private AutowiredArguments resolveArguments(RegisteredBean registeredBean, AutowireCapableBeanFactory autowireCapableBeanFactory = (AutowireCapableBeanFactory) beanFactory; int argumentCount = method.getParameterCount(); Object[] arguments = new Object[argumentCount]; - Set autowiredBeanNames = new LinkedHashSet<>(argumentCount); + Set autowiredBeanNames = CollectionUtils.newLinkedHashSet(argumentCount); TypeConverter typeConverter = beanFactory.getTypeConverter(); for (int i = 0; i < argumentCount; i++) { MethodParameter parameter = new MethodParameter(method, i); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java index 56d3e79268c7..c556eb1fe0c0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java @@ -22,7 +22,6 @@ import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; import java.util.Arrays; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -49,6 +48,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.function.ThrowingBiFunction; @@ -289,7 +289,7 @@ private ValueHolder[] resolveArgumentValues(RegisteredBean registeredBean, Execu beanFactory, registeredBean.getBeanName(), beanDefinition, beanFactory.getTypeConverter()); ConstructorArgumentValues values = resolveConstructorArguments( valueResolver, beanDefinition.getConstructorArgumentValues()); - Set usedValueHolders = new HashSet<>(parameters.length); + Set usedValueHolders = CollectionUtils.newHashSet(parameters.length); for (int i = 0; i < parameters.length; i++) { Class parameterType = parameters[i].getType(); String parameterName = (parameters[i].isNamePresent() ? parameters[i].getName() : null); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/SetFactoryBean.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/SetFactoryBean.java index 756cc1cce2b9..647af7cb8581 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/SetFactoryBean.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/SetFactoryBean.java @@ -16,13 +16,13 @@ package org.springframework.beans.factory.config; -import java.util.LinkedHashSet; import java.util.Set; import org.springframework.beans.BeanUtils; import org.springframework.beans.TypeConverter; import org.springframework.core.ResolvableType; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; /** * Simple factory for shared Set instances. Allows for central setup @@ -85,7 +85,7 @@ protected Set createInstance() { result = BeanUtils.instantiateClass(this.targetSetClass); } else { - result = new LinkedHashSet<>(this.sourceSet.size()); + result = CollectionUtils.newLinkedHashSet(this.sourceSet.size()); } Class valueType = null; if (this.targetSetClass != null) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 5883af47efd9..27439fef4a8c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -75,6 +75,7 @@ import org.springframework.core.ResolvableType; import org.springframework.lang.Nullable; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.MethodCallback; @@ -616,7 +617,7 @@ protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); - Set actualDependentBeans = new LinkedHashSet<>(dependentBeans.length); + Set actualDependentBeans = CollectionUtils.newLinkedHashSet(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); @@ -765,7 +766,7 @@ protected Class getTypeForFactoryMethod(String beanName, RootBeanDefinition m paramNames = pnd.getParameterNames(candidate); } } - Set usedValueHolders = new HashSet<>(paramTypes.length); + Set usedValueHolders = CollectionUtils.newHashSet(paramTypes.length); Object[] args = new Object[paramTypes.length]; for (int i = 0; i < args.length; i++) { ConstructorArgumentValues.ValueHolder valueHolder = cav.getArgumentValue( diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 8f2e431b09e9..f401c6b049ef 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -22,7 +22,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -74,6 +73,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.util.StringValueResolver; @@ -1153,7 +1153,7 @@ protected void beforePrototypeCreation(String beanName) { this.prototypesCurrentlyInCreation.set(beanName); } else if (curVal instanceof String strValue) { - Set beanNameSet = new HashSet<>(2); + Set beanNameSet = CollectionUtils.newHashSet(2); beanNameSet.add(strValue); beanNameSet.add(beanName); this.prototypesCurrentlyInCreation.set(beanNameSet); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java index d2d0d947d34c..1e2ceba6b90e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java @@ -466,7 +466,7 @@ private List resolveManagedList(Object argName, List ml) { * For each element in the managed set, resolve reference if necessary. */ private Set resolveManagedSet(Object argName, Set ms) { - Set resolved = new LinkedHashSet<>(ms.size()); + Set resolved = CollectionUtils.newLinkedHashSet(ms.size()); int i = 0; for (Object m : ms) { resolved.add(resolveValueIfNecessary(new KeyedArgName(argName, i), m)); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index 4e7df85fa548..172b0a06400c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -70,6 +70,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.MethodInvoker; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; @@ -598,7 +599,7 @@ else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight && } } else if (resolvedValues != null) { - Set valueHolders = new LinkedHashSet<>(resolvedValues.getArgumentCount()); + Set valueHolders = CollectionUtils.newLinkedHashSet(resolvedValues.getArgumentCount()); valueHolders.addAll(resolvedValues.getIndexedArgumentValues().values()); valueHolders.addAll(resolvedValues.getGenericArgumentValues()); for (ValueHolder value : valueHolders) { diff --git a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java index 6f1efa124def..eb0a00d27a53 100644 --- a/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java +++ b/spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBean.java @@ -28,6 +28,7 @@ import java.util.Set; import org.springframework.core.io.Resource; +import org.springframework.util.CollectionUtils; /** * @author Juergen Hoeller @@ -266,7 +267,7 @@ public Set getCustomEnumSetMismatch() { } public void setCustomEnumSetMismatch(Set customEnumSet) { - this.customEnumSet = new HashSet<>(customEnumSet.size()); + this.customEnumSet = CollectionUtils.newHashSet(customEnumSet.size()); for (String customEnumName : customEnumSet) { this.customEnumSet.add(CustomEnum.valueOf(customEnumName)); } diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java index aaee9b396b75..489628804a19 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java @@ -17,11 +17,11 @@ package org.springframework.cache.interceptor; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.Set; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; /** * Base class for cache operations. @@ -158,7 +158,7 @@ public void setCacheName(String cacheName) { } public void setCacheNames(String... cacheNames) { - this.cacheNames = new LinkedHashSet<>(cacheNames.length); + this.cacheNames = CollectionUtils.newLinkedHashSet(cacheNames.length); for (String cacheName : cacheNames) { Assert.hasText(cacheName, "Cache name must be non-empty if specified"); this.cacheNames.add(cacheName); diff --git a/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java b/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java index c7f7ef5da0aa..a7625f805d1c 100644 --- a/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java @@ -27,6 +27,7 @@ import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; /** * Abstract base class implementing the common {@link CacheManager} methods. @@ -64,7 +65,7 @@ public void initializeCaches() { synchronized (this.cacheMap) { this.cacheNames = Collections.emptySet(); this.cacheMap.clear(); - Set cacheNames = new LinkedHashSet<>(caches.size()); + Set cacheNames = CollectionUtils.newLinkedHashSet(caches.size()); for (Cache cache : caches) { String name = cache.getName(); this.cacheMap.put(name, decorateCache(cache)); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java index 903984ab0fdd..451cd1175a3a 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java @@ -17,7 +17,6 @@ package org.springframework.context.annotation; import java.lang.annotation.Annotation; -import java.util.LinkedHashSet; import java.util.Set; import java.util.function.Predicate; @@ -38,6 +37,7 @@ import org.springframework.core.type.AnnotationMetadata; import org.springframework.lang.Nullable; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; /** * Utility class that allows for convenient registration of common @@ -154,7 +154,7 @@ public static Set registerAnnotationConfigProcessors( } } - Set beanDefs = new LinkedHashSet<>(8); + Set beanDefs = CollectionUtils.newLinkedHashSet(6); if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index e8de3b323d5b..365ac4c01c6a 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -71,6 +71,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -148,7 +149,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean private static final boolean jndiPresent = ClassUtils.isPresent( "javax.naming.InitialContext", CommonAnnotationBeanPostProcessor.class.getClassLoader()); - private static final Set> resourceAnnotationTypes = new LinkedHashSet<>(4); + private static final Set> resourceAnnotationTypes = CollectionUtils.newLinkedHashSet(3); @Nullable private static final Class jakartaResourceType; diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 9a7917011b7a..699bb8d58919 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -421,7 +421,7 @@ private Set retrieveBeanMethodMetadata(SourceClass sourceClass) Set asmMethods = asm.getAnnotatedMethods(Bean.class.getName()); if (asmMethods.size() >= beanMethods.size()) { Set candidateMethods = new LinkedHashSet<>(beanMethods); - Set selectedMethods = new LinkedHashSet<>(asmMethods.size()); + Set selectedMethods = CollectionUtils.newLinkedHashSet(asmMethods.size()); for (MethodMetadata asmMethod : asmMethods) { for (Iterator it = candidateMethods.iterator(); it.hasNext();) { MethodMetadata beanMethod = it.next(); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index c150656cacc3..3d5c65f591af 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -410,7 +410,7 @@ else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this. this.resourceLoader, this.componentScanBeanNameGenerator, registry); Set candidates = new LinkedHashSet<>(configCandidates); - Set alreadyParsed = new HashSet<>(configCandidates.size()); + Set alreadyParsed = CollectionUtils.newHashSet(configCandidates.size()); do { StartupStep processConfig = this.applicationStartup.start("spring.context.config-classes.parse"); parser.parse(candidates); @@ -433,7 +433,7 @@ else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this. if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); Set oldCandidateNames = Set.of(candidateNames); - Set alreadyParsedClasses = new HashSet<>(); + Set alreadyParsedClasses = CollectionUtils.newHashSet(alreadyParsed.size()); for (ConfigurationClass configurationClass : alreadyParsed) { alreadyParsedClasses.add(configurationClass.getMetadata().getClassName()); } diff --git a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java index 50aef5f21386..49608fb63831 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java @@ -21,7 +21,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -883,7 +882,7 @@ private ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws J */ private void autodetect(Map beans, AutodetectCallback callback) { Assert.state(this.beanFactory != null, "No BeanFactory set"); - Set beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount()); + Set beanNames = CollectionUtils.newLinkedHashSet(this.beanFactory.getBeanDefinitionCount()); Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames()); if (this.beanFactory instanceof ConfigurableBeanFactory cbf) { Collections.addAll(beanNames, cbf.getSingletonNames()); diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java index 6cad78b46dd0..0a8987cd6882 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java @@ -18,7 +18,6 @@ import java.lang.annotation.Annotation; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; import java.util.concurrent.Executor; import java.util.function.Supplier; @@ -35,6 +34,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.function.SingletonSupplier; /** @@ -94,7 +94,7 @@ public AsyncAnnotationAdvisor( public AsyncAnnotationAdvisor( @Nullable Supplier executor, @Nullable Supplier exceptionHandler) { - Set> asyncAnnotationTypes = new LinkedHashSet<>(2); + Set> asyncAnnotationTypes = CollectionUtils.newLinkedHashSet(2); asyncAnnotationTypes.add(Async.class); ClassLoader classLoader = AsyncAnnotationAdvisor.class.getClassLoader(); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java index 589c6a59c9eb..601d60b12da6 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java @@ -18,7 +18,6 @@ import java.lang.reflect.Array; import java.util.Collection; -import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; @@ -26,6 +25,7 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; /** * Convert an Object to {@code java.util.Optional} if necessary using the @@ -48,7 +48,7 @@ public ObjectToOptionalConverter(ConversionService conversionService) { @Override public Set getConvertibleTypes() { - Set convertibleTypes = new LinkedHashSet<>(4); + Set convertibleTypes = CollectionUtils.newLinkedHashSet(3); convertibleTypes.add(new ConvertiblePair(Collection.class, Optional.class)); convertibleTypes.add(new ConvertiblePair(Object[].class, Optional.class)); convertibleTypes.add(new ConvertiblePair(Object.class, Optional.class)); diff --git a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java index 4c0553f0e4a3..3033cae39af0 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java @@ -24,6 +24,7 @@ import java.util.Set; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -89,7 +90,7 @@ public String[] getPropertyNames() { namesList.add(names); total += names.length; } - Set allNames = new LinkedHashSet<>(total); + Set allNames = CollectionUtils.newLinkedHashSet(total); namesList.forEach(names -> Collections.addAll(allNames, names)); return StringUtils.toStringArray(allNames); } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java index 0d7b1d6393b8..3a8a8e496654 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java @@ -30,7 +30,6 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.util.HashSet; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.Executor; @@ -54,6 +53,7 @@ import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; /** * Utility class for working with {@link DataBuffer DataBuffers}. @@ -382,7 +382,7 @@ public static Mono write(Publisher source, Path destination, O private static Set checkWriteOptions(OpenOption[] options) { int length = options.length; - Set result = new HashSet<>(length + 3); + Set result = CollectionUtils.newHashSet(length > 0 ? length : 2); if (length == 0) { result.add(StandardOpenOption.CREATE); result.add(StandardOpenOption.TRUNCATE_EXISTING); diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index bf47d0b917fd..d8d2bde43a38 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -22,8 +22,10 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; @@ -85,7 +87,7 @@ public static boolean isEmpty(@Nullable Map map) { * @see #newLinkedHashMap(int) */ public static HashMap newHashMap(int expectedSize) { - return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + return new HashMap<>(computeInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); } /** @@ -102,10 +104,34 @@ public static HashMap newHashMap(int expectedSize) { * @see #newHashMap(int) */ public static LinkedHashMap newLinkedHashMap(int expectedSize) { - return new LinkedHashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + return new LinkedHashMap<>(computeInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); } - private static int computeMapInitialCapacity(int expectedSize) { + /** + * Instantiate a new {@link HashSet} with an initial capacity + * that can accommodate the specified number of elements without + * any immediate resize/rehash operations to be expected. + * @param expectedSize the expected number of elements (with a corresponding + * capacity to be derived so that no resize/rehash operations are needed) + * @see #newLinkedHashSet(int) + */ + public static HashSet newHashSet(int expectedSize) { + return new HashSet<>(computeInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + } + + /** + * Instantiate a new {@link LinkedHashSet} with an initial capacity + * that can accommodate the specified number of elements without + * any immediate resize/rehash operations to be expected. + * @param expectedSize the expected number of elements (with a corresponding + * capacity to be derived so that no resize/rehash operations are needed) + * @see #newHashSet(int) + */ + public static LinkedHashSet newLinkedHashSet(int expectedSize) { + return new LinkedHashSet<>(computeInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + } + + private static int computeInitialCapacity(int expectedSize) { return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java index ee59e43ec413..7ae29f4a92a8 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -215,7 +214,7 @@ protected List reconcileColumnsToUse(List declaredColumns, Strin if (!declaredColumns.isEmpty()) { return new ArrayList<>(declaredColumns); } - Set keys = new LinkedHashSet<>(generatedKeyNames.length); + Set keys = CollectionUtils.newLinkedHashSet(generatedKeyNames.length); for (String key : generatedKeyNames) { keys.add(key.toUpperCase()); } @@ -296,7 +295,7 @@ public List matchInParameterValuesWithInsertColumns(Map inPar * @return the insert string to be used */ public String createInsertString(String... generatedKeyNames) { - Set keys = new LinkedHashSet<>(generatedKeyNames.length); + Set keys = CollectionUtils.newLinkedHashSet(generatedKeyNames.length); for (String key : generatedKeyNames) { keys.add(key.toUpperCase()); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java index 9c10ea0816c7..616b790dd60d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java @@ -86,7 +86,7 @@ public DestinationPatternsMessageCondition(String[] patterns, RouteMatcher route private static Set prependLeadingSlash(String[] patterns, RouteMatcher routeMatcher) { boolean slashSeparator = routeMatcher.combine("a", "a").equals("a/a"); - Set result = new LinkedHashSet<>(patterns.length); + Set result = CollectionUtils.newLinkedHashSet(patterns.length); for (String pattern : patterns) { if (slashSeparator && StringUtils.hasLength(pattern) && !pattern.startsWith("/")) { pattern = "/" + pattern; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java index 8ff7a092e994..9717a9ea477b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java @@ -30,6 +30,7 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageType; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -216,7 +217,7 @@ private Set getSessionIdsByUser(String userName, @Nullable String sessio } else { Set sessions = user.getSessions(); - sessionIds = new HashSet<>(sessions.size()); + sessionIds = CollectionUtils.newHashSet(sessions.size()); for (SimpSession session : sessions) { sessionIds.add(session.getId()); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java index b3cda62e5f6d..ecb0995c8e8e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java @@ -414,7 +414,7 @@ public TransferSimpSession(SimpSession session) { this.id = session.getId(); this.user = new TransferSimpUser(); Set subscriptions = session.getSubscriptions(); - this.subscriptions = new HashSet<>(subscriptions.size()); + this.subscriptions = CollectionUtils.newHashSet(subscriptions.size()); for (SimpSubscription subscription : subscriptions) { this.subscriptions.add(new TransferSimpSubscription(subscription)); } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java b/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java index c38787dbd2ee..06e1239eca3b 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java @@ -52,6 +52,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.MimeType; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -303,7 +304,7 @@ public Set getResourcePaths(String path) { if (ObjectUtils.isEmpty(fileList)) { return null; } - Set resourcePaths = new LinkedHashSet<>(fileList.length); + Set resourcePaths = CollectionUtils.newLinkedHashSet(fileList.length); for (String fileEntry : fileList) { String resultPath = actualPath + fileEntry; if (resource.createRelative(fileEntry).getFile().isDirectory()) { diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java index 9da77a8a70ba..5c6a3df252f8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java @@ -20,7 +20,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -53,6 +52,7 @@ import org.springframework.test.context.util.TestContextSpringFactoriesUtils; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -355,7 +355,7 @@ private Set getContextCustomizers(Class testClass, List configAttributes) { List factories = getContextCustomizerFactories(testClass); - Set customizers = new LinkedHashSet<>(factories.size()); + Set customizers = CollectionUtils.newLinkedHashSet(factories.size()); for (ContextCustomizerFactory factory : factories) { ContextCustomizer customizer = factory.createContextCustomizer(testClass, configAttributes); if (customizer != null) { diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java index 0cf4aed9627a..dbd85b90c0d8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java @@ -19,7 +19,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Executable; -import java.util.LinkedHashSet; import java.util.Set; import org.apache.commons.logging.Log; @@ -33,6 +32,7 @@ import org.springframework.test.context.TestConstructor.AutowireMode; import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; /** * Utility methods for working with {@link TestConstructor @TestConstructor}. @@ -49,7 +49,7 @@ public abstract class TestConstructorUtils { private static final Log logger = LogFactory.getLog(TestConstructorUtils.class); - private static final Set> autowiredAnnotationTypes = new LinkedHashSet<>(2); + private static final Set> autowiredAnnotationTypes = CollectionUtils.newLinkedHashSet(2); static { autowiredAnnotationTypes.add(Autowired.class); diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java index 4b6b81b7085c..29abf75a317f 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java @@ -29,6 +29,7 @@ import org.springframework.transaction.interceptor.TransactionAttribute; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; /** * Implementation of the @@ -92,7 +93,7 @@ public AnnotationTransactionAttributeSource() { public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) { this.publicMethodsOnly = publicMethodsOnly; if (jta12Present || ejb3Present) { - this.annotationParsers = new LinkedHashSet<>(4); + this.annotationParsers = CollectionUtils.newLinkedHashSet(3); this.annotationParsers.add(new SpringTransactionAnnotationParser()); if (jta12Present) { this.annotationParsers.add(new JtaTransactionAnnotationParser()); diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java index ab3df073c7c6..395b3676520f 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java @@ -18,11 +18,11 @@ import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.Set; import org.springframework.lang.Nullable; import org.springframework.transaction.support.DefaultTransactionDefinition; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.util.StringValueResolver; @@ -216,7 +216,7 @@ public void resolveAttributeStrings(@Nullable StringValueResolver resolver) { if (this.qualifier != null) { this.qualifier = resolver.resolveStringValue(this.qualifier); } - Set resolvedLabels = new LinkedHashSet<>(this.labels.size()); + Set resolvedLabels = CollectionUtils.newLinkedHashSet(this.labels.size()); for (String label : this.labels) { resolvedLabels.add(resolver.resolveStringValue(label)); } diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index d1656fbf843d..77e7567e5295 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -34,7 +34,6 @@ import java.util.Base64; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -752,7 +751,7 @@ public Set getAllow() { String value = getFirst(ALLOW); if (StringUtils.hasLength(value)) { String[] tokens = StringUtils.tokenizeToStringArray(value, ","); - Set result = new LinkedHashSet<>(tokens.length); + Set result = CollectionUtils.newLinkedHashSet(tokens.length); for (String token : tokens) { HttpMethod method = HttpMethod.valueOf(token); result.add(method); diff --git a/spring-web/src/main/java/org/springframework/http/support/HttpComponentsHeadersAdapter.java b/spring-web/src/main/java/org/springframework/http/support/HttpComponentsHeadersAdapter.java index 9fd80f6816c5..ed3d18c992a9 100644 --- a/spring-web/src/main/java/org/springframework/http/support/HttpComponentsHeadersAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/support/HttpComponentsHeadersAdapter.java @@ -22,7 +22,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -163,7 +162,7 @@ public void clear() { @Override public Set keySet() { - Set keys = new LinkedHashSet<>(size()); + Set keys = CollectionUtils.newLinkedHashSet(size()); for (Header header : this.message.getHeaders()) { keys.add(header.getName()); } diff --git a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java index cf667bae3e40..4a1fcb95421b 100644 --- a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java +++ b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java @@ -17,7 +17,6 @@ package org.springframework.web; import java.util.Collection; -import java.util.LinkedHashSet; import java.util.Set; import jakarta.servlet.ServletException; @@ -28,6 +27,7 @@ import org.springframework.http.HttpStatusCode; import org.springframework.http.ProblemDetail; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -107,7 +107,7 @@ public Set getSupportedHttpMethods() { if (this.supportedMethods == null) { return null; } - Set supportedMethods = new LinkedHashSet<>(this.supportedMethods.length); + Set supportedMethods = CollectionUtils.newLinkedHashSet(this.supportedMethods.length); for (String value : this.supportedMethods) { HttpMethod method = HttpMethod.valueOf(value); supportedMethods.add(method); diff --git a/spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java b/spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java index 6e2f05e99aa9..9786f9dec8b3 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java +++ b/spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -29,6 +28,7 @@ import org.springframework.http.MediaType; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; /** * An implementation of {@code MediaTypeFileExtensionResolver} that maintains @@ -55,7 +55,7 @@ public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExten */ public MappingMediaTypeFileExtensionResolver(@Nullable Map mediaTypes) { if (mediaTypes != null) { - Set allFileExtensions = new HashSet<>(mediaTypes.size()); + Set allFileExtensions = CollectionUtils.newHashSet(mediaTypes.size()); mediaTypes.forEach((extension, mediaType) -> { String lowerCaseExtension = extension.toLowerCase(Locale.ENGLISH); this.mediaTypes.put(lowerCaseExtension, mediaType); diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java index dbb22c425328..d66fe2f63eae 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java @@ -19,7 +19,6 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.function.Consumer; @@ -657,7 +656,7 @@ private List combine(@Nullable List source, @Nullable List combined = new LinkedHashSet<>(source.size() + other.size()); + Set combined = CollectionUtils.newLinkedHashSet(source.size() + other.size()); combined.addAll(source); combined.addAll(other); return new ArrayList<>(combined); @@ -675,7 +674,7 @@ private List combinePatterns( if (source.contains(ALL_PATTERN) || other.contains(ALL_PATTERN)) { return ALL_PATTERN_LIST; } - Set combined = new LinkedHashSet<>(source.size() + other.size()); + Set combined = CollectionUtils.newLinkedHashSet(source.size() + other.size()); combined.addAll(source); combined.addAll(other); return new ArrayList<>(combined); diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java index 6d7bd180bfc5..70280d6cbe64 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java @@ -37,6 +37,7 @@ import org.springframework.http.ContentDisposition; import org.springframework.http.HttpHeaders; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -91,7 +92,7 @@ public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean l private void parseRequest(HttpServletRequest request) { try { Collection parts = request.getParts(); - this.multipartParameterNames = new LinkedHashSet<>(parts.size()); + this.multipartParameterNames = CollectionUtils.newLinkedHashSet(parts.size()); MultiValueMap files = new LinkedMultiValueMap<>(parts.size()); for (Part part : parts) { String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION); diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockServletContext.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockServletContext.java index b5630023d090..f94a1b80e02e 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockServletContext.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockServletContext.java @@ -52,6 +52,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; import org.springframework.util.MimeType; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -303,7 +304,7 @@ public Set getResourcePaths(String path) { if (ObjectUtils.isEmpty(fileList)) { return null; } - Set resourcePaths = new LinkedHashSet<>(fileList.length); + Set resourcePaths = CollectionUtils.newLinkedHashSet(fileList.length); for (String fileEntry : fileList) { String resultPath = actualPath + fileEntry; if (resource.createRelative(fileEntry).getFile().isDirectory()) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/HeadersRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/HeadersRequestCondition.java index 8a785dcb0ff5..97269a6dd578 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/HeadersRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/HeadersRequestCondition.java @@ -22,6 +22,7 @@ import java.util.Set; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.cors.reactive.CorsUtils; @@ -65,7 +66,7 @@ private static Set parseExpressions(String... headers) { if ("Accept".equalsIgnoreCase(expr.name) || "Content-Type".equalsIgnoreCase(expr.name)) { continue; } - result = (result != null ? result : new LinkedHashSet<>(headers.length)); + result = (result != null ? result : CollectionUtils.newLinkedHashSet(headers.length)); result.add(expr); } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ParamsRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ParamsRequestCondition.java index 98fa9c42e374..e590b1df7a62 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ParamsRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ParamsRequestCondition.java @@ -22,6 +22,7 @@ import java.util.Set; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.server.ServerWebExchange; @@ -51,7 +52,7 @@ private static Set parseExpressions(String... params) { if (ObjectUtils.isEmpty(params)) { return Collections.emptySet(); } - Set result = new LinkedHashSet<>(params.length); + Set result = CollectionUtils.newLinkedHashSet(params.length); for (String param : params) { result.add(new ParamExpression(param)); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/HeadersRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/HeadersRequestCondition.java index 15408eb1e257..7b930e516039 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/HeadersRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/HeadersRequestCondition.java @@ -24,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.cors.CorsUtils; @@ -67,7 +68,7 @@ private static Set parseExpressions(String... headers) { if ("Accept".equalsIgnoreCase(expr.name) || "Content-Type".equalsIgnoreCase(expr.name)) { continue; } - result = (result != null ? result : new LinkedHashSet<>(headers.length)); + result = (result != null ? result : CollectionUtils.newLinkedHashSet(headers.length)); result.add(expr); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ParamsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ParamsRequestCondition.java index 590c71daa7df..230c8d1b2090 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ParamsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ParamsRequestCondition.java @@ -25,6 +25,7 @@ import jakarta.servlet.http.HttpServletRequest; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.util.WebUtils; @@ -55,7 +56,7 @@ private static Set parseExpressions(String... params) { if (ObjectUtils.isEmpty(params)) { return Collections.emptySet(); } - Set expressions = new LinkedHashSet<>(params.length); + Set expressions = CollectionUtils.newLinkedHashSet(params.length); for (String param : params) { expressions.add(new ParamExpression(param)); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java index f642c72534ff..8fadbc848c83 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java @@ -30,6 +30,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.AntPathMatcher; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.PathMatcher; import org.springframework.util.StringUtils; @@ -157,7 +158,7 @@ private static Set initPatterns(String[] patterns) { if (!hasPattern(patterns)) { return EMPTY_PATH_PATTERN; } - Set result = new LinkedHashSet<>(patterns.length); + Set result = CollectionUtils.newLinkedHashSet(patterns.length); for (String pattern : patterns) { pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern); result.add(pattern); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index ea3d01849ced..97a991360cfe 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -512,7 +512,7 @@ public HttpOptionsHandler(Set declaredMethods, Set acceptPatc } private static Set initAllowedHttpMethods(Set declaredMethods) { - Set result = new LinkedHashSet<>(declaredMethods.size()); + Set result = CollectionUtils.newLinkedHashSet(declaredMethods.size()); if (declaredMethods.isEmpty()) { for (HttpMethod method : HttpMethod.values()) { if (method != HttpMethod.TRACE) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java index 52acc16047d8..97343475c74f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java @@ -32,6 +32,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.HttpRequestMethodNotSupportedException; @@ -130,7 +131,7 @@ public WebContentGenerator() { */ public WebContentGenerator(boolean restrictDefaultSupportedMethods) { if (restrictDefaultSupportedMethods) { - this.supportedMethods = new LinkedHashSet<>(4); + this.supportedMethods = CollectionUtils.newLinkedHashSet(3); this.supportedMethods.add(METHOD_GET); this.supportedMethods.add(METHOD_HEAD); this.supportedMethods.add(METHOD_POST);