Polish Stream#toList() consistent usage

In general, `Stream#toList()` is not a transparent replacement for
`.collect(Collectors.toList()))`, as the former returns an immutable
list.

This commit reverts some of those changes, where the returned `List`
instance was expected to be mutable.

See gh-29203
This commit is contained in:
Brian Clozel 2022-09-26 20:25:13 +02:00
parent 641303baff
commit 9c400ed384
3 changed files with 8 additions and 5 deletions

View File

@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import org.springframework.lang.Nullable;
@ -279,7 +280,7 @@ public abstract class MimeTypeUtils {
}
/**
* Parse the comma-separated string into a list of {@code MimeType} objects.
* Parse the comma-separated string into a mutable list of {@code MimeType} objects.
* @param mimeTypes the string to parse
* @return the list of mime types
* @throws InvalidMimeTypeException if the string cannot be parsed
@ -291,7 +292,7 @@ public abstract class MimeTypeUtils {
return tokenize(mimeTypes).stream()
.filter(StringUtils::hasText)
.map(MimeTypeUtils::parseMimeType)
.toList();
.collect(Collectors.toList());
}
/**

View File

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import io.micrometer.observation.Observation;
@ -970,7 +971,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
.filter(converter -> canReadResponse(this.responseType, converter))
.flatMap((HttpMessageConverter<?> converter) -> getSupportedMediaTypes(this.responseType, converter))
.distinct()
.toList();
.collect(Collectors.toList());
MimeTypeUtils.sortBySpecificity(allSupportedMediaTypes);
if (logger.isDebugEnabled()) {
logger.debug("Accept=" + allSupportedMediaTypes);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
@ -135,7 +136,7 @@ public class CorsConfiguration {
*/
public void setAllowedOrigins(@Nullable List<String> origins) {
this.allowedOrigins = (origins == null ? null :
origins.stream().filter(Objects::nonNull).map(this::trimTrailingSlash).toList());
origins.stream().filter(Objects::nonNull).map(this::trimTrailingSlash).collect(Collectors.toList()));
}
private String trimTrailingSlash(String origin) {