Use ConcurrentHashMap.newKeySet

In places where a ConcurrentHashMap was used as a set by wrapping it
with Collections.newSetFromMap, switch to just using the set returned
by ConcurrentHashMap.newKeySet directly.

Closes gh-32294
This commit is contained in:
Patrick Strawderman
2024-02-19 08:57:33 -08:00
committed by Sam Brannen
parent ff9c7141c5
commit f9fe8efb2e
14 changed files with 16 additions and 29 deletions

View File

@@ -20,7 +20,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -136,7 +135,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
@Nullable @Nullable
private BeanFactory beanFactory; private BeanFactory beanFactory;
private final Set<String> targetSourcedBeans = Collections.newSetFromMap(new ConcurrentHashMap<>(16)); private final Set<String> targetSourcedBeans = ConcurrentHashMap.newKeySet(16);
private final Map<Object, Object> earlyBeanReferences = new ConcurrentHashMap<>(16); private final Map<Object, Object> earlyBeanReferences = new ConcurrentHashMap<>(16);

View File

@@ -23,7 +23,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URL; import java.net.URL;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@@ -88,7 +87,7 @@ public final class CachedIntrospectionResults {
* accept classes from, even if the classes do not qualify as cache-safe. * accept classes from, even if the classes do not qualify as cache-safe.
*/ */
static final Set<ClassLoader> acceptedClassLoaders = static final Set<ClassLoader> acceptedClassLoaders =
Collections.newSetFromMap(new ConcurrentHashMap<>(16)); ConcurrentHashMap.newKeySet(16);
/** /**
* Map keyed by Class containing CachedIntrospectionResults, strongly held. * Map keyed by Class containing CachedIntrospectionResults, strongly held.

View File

@@ -29,7 +29,6 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@@ -178,7 +177,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
@Nullable @Nullable
private MetadataReaderFactory metadataReaderFactory; private MetadataReaderFactory metadataReaderFactory;
private final Set<String> lookupMethodsChecked = Collections.newSetFromMap(new ConcurrentHashMap<>(256)); private final Set<String> lookupMethodsChecked = ConcurrentHashMap.newKeySet(256);
private final Map<Class<?>, Constructor<?>[]> candidateConstructorsCache = new ConcurrentHashMap<>(256); private final Map<Class<?>, Constructor<?>[]> candidateConstructorsCache = new ConcurrentHashMap<>(256);

View File

@@ -16,7 +16,6 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@@ -77,7 +76,7 @@ public class PropertyOverrideConfigurer extends PropertyResourceConfigurer {
/** /**
* Contains names of beans that have overrides. * Contains names of beans that have overrides.
*/ */
private final Set<String> beanNames = Collections.newSetFromMap(new ConcurrentHashMap<>(16)); private final Set<String> beanNames = ConcurrentHashMap.newKeySet(16);
/** /**

View File

@@ -20,7 +20,6 @@ import java.beans.PropertyEditor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@@ -165,7 +164,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<>(256); private final Map<String, RootBeanDefinition> mergedBeanDefinitions = new ConcurrentHashMap<>(256);
/** Names of beans that have already been created at least once. */ /** Names of beans that have already been created at least once. */
private final Set<String> alreadyCreated = Collections.newSetFromMap(new ConcurrentHashMap<>(256)); private final Set<String> alreadyCreated = ConcurrentHashMap.newKeySet(256);
/** Names of beans that are currently in creation. */ /** Names of beans that are currently in creation. */
private final ThreadLocal<Object> prototypesCurrentlyInCreation = private final ThreadLocal<Object> prototypesCurrentlyInCreation =

View File

@@ -29,7 +29,6 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Iterator; import java.util.Iterator;
@@ -169,7 +168,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256); private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256);
// Set of bean definition names with a primary marker. */ // Set of bean definition names with a primary marker. */
private final Set<String> primaryBeanNames = Collections.newSetFromMap(new ConcurrentHashMap<>(16)); private final Set<String> primaryBeanNames = ConcurrentHashMap.newKeySet(16);
/** Map of singleton and non-singleton bean names, keyed by dependency type. */ /** Map of singleton and non-singleton bean names, keyed by dependency type. */
private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64); private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);

View File

@@ -16,7 +16,6 @@
package org.springframework.beans.factory.support; package org.springframework.beans.factory.support;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@@ -91,12 +90,10 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
private final Lock singletonLock = new ReentrantLock(); private final Lock singletonLock = new ReentrantLock();
/** Names of beans that are currently in creation. */ /** Names of beans that are currently in creation. */
private final Set<String> singletonsCurrentlyInCreation = private final Set<String> singletonsCurrentlyInCreation = ConcurrentHashMap.newKeySet(16);
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
/** Names of beans currently excluded from in creation checks. */ /** Names of beans currently excluded from in creation checks. */
private final Set<String> inCreationCheckExclusions = private final Set<String> inCreationCheckExclusions = ConcurrentHashMap.newKeySet(16);
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
@Nullable @Nullable
private volatile Thread singletonCreationThread; private volatile Thread singletonCreationThread;

View File

@@ -18,7 +18,6 @@ package org.springframework.context.event;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -82,7 +81,7 @@ public class EventListenerMethodProcessor
@Nullable @Nullable
private final EventExpressionEvaluator evaluator; private final EventExpressionEvaluator evaluator;
private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64)); private final Set<Class<?>> nonAnnotatedClasses = ConcurrentHashMap.newKeySet(64);
public EventListenerMethodProcessor() { public EventListenerMethodProcessor() {

View File

@@ -229,7 +229,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
void stopForRestart() { void stopForRestart() {
if (this.running) { if (this.running) {
this.stoppedBeans = Collections.newSetFromMap(new ConcurrentHashMap<>()); this.stoppedBeans = ConcurrentHashMap.newKeySet();
stopBeans(); stopBeans();
this.running = false; this.running = false;
} }

View File

@@ -20,7 +20,6 @@ import java.lang.reflect.Method;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@@ -149,7 +148,7 @@ public class ScheduledAnnotationBeanPostProcessor
@Nullable @Nullable
private TaskSchedulerRouter localScheduler; private TaskSchedulerRouter localScheduler;
private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64)); private final Set<Class<?>> nonAnnotatedClasses = ConcurrentHashMap.newKeySet(64);
private final Map<Object, Set<ScheduledTask>> scheduledTasks = new IdentityHashMap<>(16); private final Map<Object, Set<ScheduledTask>> scheduledTasks = new IdentityHashMap<>(16);

View File

@@ -16,7 +16,6 @@
package org.springframework.core; package org.springframework.core;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -39,9 +38,9 @@ public abstract class DecoratingClassLoader extends ClassLoader {
} }
private final Set<String> excludedPackages = Collections.newSetFromMap(new ConcurrentHashMap<>(8)); private final Set<String> excludedPackages = ConcurrentHashMap.newKeySet(8);
private final Set<String> excludedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(8)); private final Set<String> excludedClasses = ConcurrentHashMap.newKeySet(8);
/** /**

View File

@@ -17,7 +17,6 @@
package org.springframework.core.task; package org.springframework.core.task;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -183,7 +182,7 @@ public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator
public void setTaskTerminationTimeout(long timeout) { public void setTaskTerminationTimeout(long timeout) {
Assert.isTrue(timeout >= 0, "Timeout value must be >=0"); Assert.isTrue(timeout >= 0, "Timeout value must be >=0");
this.taskTerminationTimeout = timeout; this.taskTerminationTimeout = timeout;
this.activeThreads = (timeout > 0 ? Collections.newSetFromMap(new ConcurrentHashMap<>()) : null); this.activeThreads = (timeout > 0 ? ConcurrentHashMap.newKeySet() : null);
} }
/** /**

View File

@@ -64,7 +64,7 @@ public class SessionAttributesHandler {
private final Set<Class<?>> attributeTypes = new HashSet<>(); private final Set<Class<?>> attributeTypes = new HashSet<>();
private final Set<String> knownAttributeNames = Collections.newSetFromMap(new ConcurrentHashMap<>(4)); private final Set<String> knownAttributeNames = ConcurrentHashMap.newKeySet(4);
private final SessionAttributeStore sessionAttributeStore; private final SessionAttributeStore sessionAttributeStore;

View File

@@ -42,7 +42,7 @@ class SessionAttributesHandler {
private final Set<Class<?>> attributeTypes = new HashSet<>(); private final Set<Class<?>> attributeTypes = new HashSet<>();
private final Set<String> knownAttributeNames = Collections.newSetFromMap(new ConcurrentHashMap<>(4)); private final Set<String> knownAttributeNames = ConcurrentHashMap.newKeySet(4);
/** /**