diff --git a/spring-core/src/main/java/org/springframework/core/Constants.java b/spring-core/src/main/java/org/springframework/core/Constants.java index a3a70f2e7ef7..45ba8d7a855d 100644 --- a/spring-core/src/main/java/org/springframework/core/Constants.java +++ b/spring-core/src/main/java/org/springframework/core/Constants.java @@ -22,6 +22,8 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -214,13 +216,18 @@ public Set getNamesForSuffix(@Nullable String nameSuffix) { */ public Set getValues(@Nullable String namePrefix) { String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : ""); - Set values = new HashSet<>(); - for (String code : this.fieldCache.keySet()) { - if (code.startsWith(prefixToUse)) { - values.add(this.fieldCache.get(code)); - } - } - return values; + return getValues(code -> code.getKey().startsWith(prefixToUse)); + } + + /** + * Return all values of the given group of constants. + * @param predicate filter values by lambda function + * @return the set of values + */ + private Set getValues(Predicate> predicate) { + return this.fieldCache.entrySet().stream(). + filter(predicate). + map(Map.Entry::getValue).collect(Collectors.toSet()); } /** @@ -246,13 +253,7 @@ public Set getValuesForProperty(String propertyName) { */ public Set getValuesForSuffix(@Nullable String nameSuffix) { String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : ""); - Set values = new HashSet<>(); - for (String code : this.fieldCache.keySet()) { - if (code.endsWith(suffixToUse)) { - values.add(this.fieldCache.get(code)); - } - } - return values; + return getValues(code -> code.getKey().endsWith(suffixToUse)); } diff --git a/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java b/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java index 0e676c43eb5f..35fdd3d61733 100644 --- a/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java +++ b/spring-core/src/main/java/org/springframework/core/SimpleAliasRegistry.java @@ -141,8 +141,7 @@ public void resolveAliases(StringValueResolver valueResolver) { Assert.notNull(valueResolver, "StringValueResolver must not be null"); synchronized (this.aliasMap) { Map aliasCopy = new HashMap<>(this.aliasMap); - for (String alias : aliasCopy.keySet()) { - String registeredName = aliasCopy.get(alias); + aliasCopy.forEach((alias, registeredName) -> { String resolvedAlias = valueResolver.resolveStringValue(alias); String resolvedName = valueResolver.resolveStringValue(registeredName); if (resolvedAlias == null || resolvedName == null || resolvedAlias.equals(resolvedName)) { @@ -154,7 +153,7 @@ else if (!resolvedAlias.equals(alias)) { if (existingName.equals(resolvedName)) { // Pointing to existing alias - just remove placeholder this.aliasMap.remove(alias); - break; + return; } throw new IllegalStateException( "Cannot register resolved alias '" + resolvedAlias + "' (original: '" + alias + @@ -168,7 +167,7 @@ else if (!resolvedAlias.equals(alias)) { else if (!registeredName.equals(resolvedName)) { this.aliasMap.put(alias, resolvedName); } - } + }); } } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 59c6cf5327e3..f2a32e170320 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -1246,14 +1246,14 @@ static void postProcessAnnotationAttributes(@Nullable Object annotatedElement, if (!attributes.validated) { // Validate @AliasFor configuration Map> aliasMap = getAttributeAliasMap(annotationType); - for (String attributeName : aliasMap.keySet()) { + aliasMap.forEach((attributeName, aliasedAttributeNames) -> { if (valuesAlreadyReplaced.contains(attributeName)) { - continue; + return; } Object value = attributes.get(attributeName); boolean valuePresent = (value != null && !(value instanceof DefaultValueHolder)); - for (String aliasedAttributeName : aliasMap.get(attributeName)) { + for (String aliasedAttributeName : aliasedAttributeNames) { if (valuesAlreadyReplaced.contains(aliasedAttributeName)) { continue; } @@ -1290,7 +1290,7 @@ else if (aliasPresent) { } } } - } + }); attributes.validated = true; } diff --git a/spring-core/src/main/java/org/springframework/util/MimeType.java b/spring-core/src/main/java/org/springframework/util/MimeType.java index bc2d996ab824..d8622c508ede 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeType.java +++ b/spring-core/src/main/java/org/springframework/util/MimeType.java @@ -409,16 +409,16 @@ private boolean parametersAreEqual(MimeType other) { return false; } - for (String key : this.parameters.keySet()) { - if (!other.parameters.containsKey(key)) { + for (Map.Entry entry : this.parameters.entrySet()) { + if (!other.parameters.containsKey(entry.getKey())) { return false; } - if (PARAM_CHARSET.equals(key)) { + if (PARAM_CHARSET.equals(entry.getKey())) { if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) { return false; } } - else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) { + else if (!ObjectUtils.nullSafeEquals(entry.getValue(), other.parameters.get(entry.getKey()))) { return false; } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java index bcf39c992640..6724585ff3a2 100755 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java @@ -563,12 +563,12 @@ public Map matchInParameterValuesWithCallParameters(SqlParameter } Map matchedParameters = new HashMap<>(inParameters.size()); - for (String parameterName : inParameters.keySet()) { + inParameters.forEach((parameterName, parameterValue) -> { String parameterNameToMatch = provider.parameterNameToUse(parameterName); String callParameterName = callParameterNames.get(lowerCase(parameterNameToMatch)); if (callParameterName == null) { if (logger.isDebugEnabled()) { - Object value = inParameters.get(parameterName); + Object value = parameterValue; if (value instanceof SqlParameterValue) { value = ((SqlParameterValue)value).getValue(); } @@ -579,9 +579,9 @@ public Map matchInParameterValuesWithCallParameters(SqlParameter } } else { - matchedParameters.put(callParameterName, inParameters.get(parameterName)); + matchedParameters.put(callParameterName, parameterValue); } - } + }); if (matchedParameters.size() < callParameterNames.size()) { for (String parameterName : callParameterNames.keySet()) { 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 10823a73b19c..38f070585ea9 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 @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -244,15 +245,9 @@ public List matchInParameterValuesWithInsertColumns(SqlParameterSource p * @param inParameters the parameter names and values */ public List matchInParameterValuesWithInsertColumns(Map inParameters) { - List values = new ArrayList<>(); Map source = new LinkedHashMap<>(inParameters.size()); - for (String key : inParameters.keySet()) { - source.put(key.toLowerCase(), inParameters.get(key)); - } - for (String column : this.tableColumns) { - values.add(source.get(column.toLowerCase())); - } - return values; + inParameters.forEach((key, value) -> source.put(key.toLowerCase(), value)); + return this.tableColumns.stream().map(column -> source.get(column.toLowerCase())).collect(Collectors.toList()); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistrar.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistrar.java index 02ebbfc1adbe..ad8d5535829c 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistrar.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/CustomSQLExceptionTranslatorRegistrar.java @@ -50,9 +50,8 @@ public void setTranslators(Map translators) { @Override public void afterPropertiesSet() { - for (String dbName : this.translators.keySet()) { - CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, this.translators.get(dbName)); - } + this.translators.forEach((dbName, translator) -> + CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, translator)); } }