Skip to content

Manual addition to a collection could be replaced with the use of Collections.addAll #2277

@zibbly

Description

@zibbly

Using a for loop to add elements to a collection could be replaced in the instances below by use of the Collections.addAll method. This method does the same thing but the for loop does not need to be duplicated in the Spring Boot code base to achieve the same result.

org.springframework.boot.actuate.metrics.reader.CompositeMetricReader

public CompositeMetricReader(MetricReader... readers) {
    for (MetricReader reader : readers) {
        this.readers.add(reader);
    }
}

to

Collections.addAll(this.readers, readers);

org.springframework.boot.actuate.metrics.writer.CompositeMetricWriter

public CompositeMetricWriter(MetricWriter... writers) {
    for (MetricWriter writer : writers) {
        this.writers.add(writer);
    }
}

to

Collections.addAll(this.writers, writers);

org.springframework.boot.loader.ExecutableArchiveLauncher.createClassLoader(URL[])

    for (URL url : urls) {
        copy.add(url);
    }

to

    Collections.addAll(copy, urls);

org.springframework.boot.context.embedded.FilterRegistrationBean(Filter, ServletRegistrationBean...)

    for (ServletRegistrationBean servletRegistrationBean : servletRegistrationBeans) {
        this.servletRegistrationBeans.add(servletRegistrationBean);
    }

to

    Collections.addAll(this.servletRegistrationBeans, servletRegistrationBeans);

org.springframework.boot.context.embedded.FilterRegistrationBean.addServletRegistrationBeans(ServletRegistrationBean...)

    for (ServletRegistrationBean servletRegistrationBean : servletRegistrationBeans) {
        this.servletRegistrationBeans.add(servletRegistrationBean);
    }

to

    Collections.addAll(this.servletRegistrationBeans, servletRegistrationBeans);

org.springframework.boot.context.embedded.FilterRegistrationBean.addUrlPatterns(String...)

    for (String urlPattern : urlPatterns) {
        this.urlPatterns.add(urlPattern);
    }

to

    Collections.addAll(this.urlPatterns, urlPatterns);

org.springframework.boot.autoconfigure.condition.BeanSearchSpec.collect(MultiValueMap<String, Object>, String key, List)

            for (String value : valueArray) {
                destination.add(value);
            }

to

            Collections.addAll(destination, valueArray);

org.springframework.boot.yaml.SpringProfileDocumentMatcher.addActiveProfiles(String...)

    for (String profile : profiles) {
        set.add(profile);
    }

to

    Collections.addAll(set, profiles);

org.springframework.boot.cli.command.core.HintCommand.run(String...)

        for (int i = 2; i < args.length; i++) {
            arguments.add(args[i]);
        }

to

        arguments.addAll(Arrays.asList(args).subList(2, args.length));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions