From 1ef5f61ab2b4a88716444f3b513157a5cbb5e190 Mon Sep 17 00:00:00 2001 From: diguage Date: Tue, 13 Jun 2017 17:53:13 +0800 Subject: [PATCH 1/2] Refactor iterator of Map with Java8's Map.forEach See gh-1459 --- .../context/index/PropertiesMarshaller.java | 10 ++++---- .../cache/caffeine/CaffeineCacheManager.java | 5 +--- .../namedparam/MapSqlParameterSource.java | 11 ++++----- .../lookup/AbstractRoutingDataSource.java | 8 +++---- ...msListenerAnnotationBeanPostProcessor.java | 8 ++----- .../MappingJackson2MessageConverter.java | 6 ++--- .../messaging/MessageHeaders.java | 16 ++++++------- .../AbstractMethodMessageHandler.java | 4 +--- .../messaging/simp/stomp/StompHeaders.java | 17 ++++--------- .../support/MessageHeaderAccessor.java | 16 ++++++------- .../support/NativeMessageHeaderAccessor.java | 6 +---- .../jpa/AbstractEntityManagerFactoryBean.java | 8 +++---- .../oxm/castor/CastorMarshaller.java | 20 ++++------------ .../oxm/xstream/XStreamMarshaller.java | 24 ++++++++----------- .../htmlunit/HtmlUnitRequestBuilder.java | 20 +++++----------- .../MockHttpServletRequestBuilder.java | 15 ++++-------- .../MethodMapTransactionAttributeSource.java | 4 +--- .../NameMatchTransactionAttributeSource.java | 4 +--- 18 files changed, 72 insertions(+), 130 deletions(-) diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/PropertiesMarshaller.java b/spring-context-indexer/src/main/java/org/springframework/context/index/PropertiesMarshaller.java index 3ee45cd75b..7505963cd5 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/PropertiesMarshaller.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/PropertiesMarshaller.java @@ -21,7 +21,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.HashSet; -import java.util.Map; import java.util.Properties; import java.util.Set; @@ -43,11 +42,10 @@ abstract class PropertiesMarshaller { CandidateComponentsMetadata result = new CandidateComponentsMetadata(); Properties props = new Properties(); props.load(in); - for (Map.Entry entry : props.entrySet()) { - String type = (String) entry.getKey(); - Set candidates = new HashSet<>(Arrays.asList(((String) entry.getValue()).split(","))); - result.add(new ItemMetadata(type, candidates)); - } + props.forEach((type, value) -> { + Set candidates = new HashSet<>(Arrays.asList(((String) value).split(","))); + result.add(new ItemMetadata((String) type, candidates)); + }); return result; } diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index f09322f602..16b616e5c1 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -19,7 +19,6 @@ package org.springframework.cache.caffeine; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -223,9 +222,7 @@ public class CaffeineCacheManager implements CacheManager { * Create the known caches again with the current state of this manager. */ private void refreshKnownCaches() { - for (Map.Entry entry : this.cacheMap.entrySet()) { - entry.setValue(createCaffeineCache(entry.getKey())); - } + this.cacheMap.forEach((key, value) -> createCaffeineCache(key)); } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java index 88df516c58..886345f40f 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java @@ -131,13 +131,12 @@ public class MapSqlParameterSource extends AbstractSqlParameterSource { */ public MapSqlParameterSource addValues(@Nullable Map values) { if (values != null) { - for (Map.Entry entry : values.entrySet()) { - this.values.put(entry.getKey(), entry.getValue()); - if (entry.getValue() instanceof SqlParameterValue) { - SqlParameterValue value = (SqlParameterValue) entry.getValue(); - registerSqlType(entry.getKey(), value.getSqlType()); + values.forEach((key, value) -> { + this.values.put(key, value); + if (value instanceof SqlParameterValue) { + registerSqlType(key, ((SqlParameterValue) value).getSqlType()); } - } + }); } return this; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java index d68b8aa824..f0983a3bd3 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java @@ -114,11 +114,11 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple throw new IllegalArgumentException("Property 'targetDataSources' is required"); } this.resolvedDataSources = new HashMap<>(this.targetDataSources.size()); - for (Map.Entry entry : this.targetDataSources.entrySet()) { - Object lookupKey = resolveSpecifiedLookupKey(entry.getKey()); - DataSource dataSource = resolveSpecifiedDataSource(entry.getValue()); + this.targetDataSources.forEach((key, value) -> { + Object lookupKey = resolveSpecifiedLookupKey(key); + DataSource dataSource = resolveSpecifiedDataSource(value); this.resolvedDataSources.put(lookupKey, dataSource); - } + }); if (this.defaultTargetDataSource != null) { this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource); } diff --git a/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java b/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java index e52c8dc0e7..cdec528d39 100644 --- a/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java +++ b/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java @@ -225,12 +225,8 @@ public class JmsListenerAnnotationBeanPostProcessor } else { // Non-empty set of methods - for (Map.Entry> entry : annotatedMethods.entrySet()) { - Method method = entry.getKey(); - for (JmsListener listener : entry.getValue()) { - processJmsListener(listener, method, bean); - } - } + annotatedMethods.forEach((method, listeners) -> + listeners.forEach(listener -> processJmsListener(listener, method, bean))); if (logger.isDebugEnabled()) { logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName + "': " + annotatedMethods); diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java index 50d8e64908..5f74a792a7 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java @@ -158,12 +158,10 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B */ public void setTypeIdMappings(Map> typeIdMappings) { this.idClassMappings = new HashMap<>(); - for (Map.Entry> entry : typeIdMappings.entrySet()) { - String id = entry.getKey(); - Class clazz = entry.getValue(); + typeIdMappings.forEach((id, clazz) -> { this.idClassMappings.put(id, clazz); this.classIdMappings.put(clazz, id); - } + }); } @Override diff --git a/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java b/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java index 449817b16c..38e2bae9af 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java @@ -149,11 +149,11 @@ public class MessageHeaders implements Map, Serializable { */ private MessageHeaders(MessageHeaders original, Set keysToIgnore) { this.headers = new HashMap<>(original.headers.size() - keysToIgnore.size()); - for (Map.Entry entry : original.headers.entrySet()) { - if (!keysToIgnore.contains(entry.getKey())) { - this.headers.put(entry.getKey(), entry.getValue()); + original.headers.forEach((key, value) -> { + if (!keysToIgnore.contains(key)) { + this.headers.put(key, value); } - } + }); } @@ -276,11 +276,11 @@ public class MessageHeaders implements Map, Serializable { private void writeObject(ObjectOutputStream out) throws IOException { Set keysToIgnore = new HashSet<>(); - for (Map.Entry entry : this.headers.entrySet()) { - if (!(entry.getValue() instanceof Serializable)) { - keysToIgnore.add(entry.getKey()); + this.headers.forEach((key, value) -> { + if (!(value instanceof Serializable)) { + keysToIgnore.add(key); } - } + }); if (keysToIgnore.isEmpty()) { // All entries are serializable -> serialize the regular MessageHeaders instance diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java index 9272d3e56f..06f7896815 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java @@ -288,9 +288,7 @@ public abstract class AbstractMethodMessageHandler if (logger.isDebugEnabled()) { logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods); } - for (Map.Entry entry : methods.entrySet()) { - registerHandlerMethod(handler, entry.getKey(), entry.getValue()); - } + methods.forEach((key, value) -> registerHandlerMethod(handler, key, value)); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java index d02ba28b57..6c3cfe43b8 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java @@ -113,10 +113,7 @@ public class StompHeaders implements MultiValueMap, Serializable Assert.notNull(headers, "'headers' must not be null"); if (readOnly) { Map> map = new LinkedMultiValueMap<>(headers.size()); - for (Entry> entry : headers.entrySet()) { - List values = Collections.unmodifiableList(entry.getValue()); - map.put(entry.getKey(), values); - } + headers.forEach((key, value) -> map.put(key, Collections.unmodifiableList(value))); this.headers = Collections.unmodifiableMap(map); } else { @@ -424,9 +421,7 @@ public class StompHeaders implements MultiValueMap, Serializable @Override public void addAll(MultiValueMap values) { - for (Entry> entry : values.entrySet()) { - addAll(entry.getKey(), entry.getValue()); - } + values.forEach(this::addAll); } /** @@ -446,17 +441,13 @@ public class StompHeaders implements MultiValueMap, Serializable @Override public void setAll(Map values) { - for (Entry entry : values.entrySet()) { - set(entry.getKey(), entry.getValue()); - } + values.forEach(this::set); } @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap<>(this.headers.size()); - for (Entry> entry : headers.entrySet()) { - singleValueMap.put(entry.getKey(), entry.getValue().get(0)); - } + headers.forEach((key, value) -> singleValueMap.put(key, value.get(0))); return singleValueMap; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java index e595f2dad5..0a764f1a48 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java @@ -387,11 +387,11 @@ public class MessageHeaderAccessor { */ public void copyHeaders(@Nullable Map headersToCopy) { if (headersToCopy != null) { - for (Map.Entry entry : headersToCopy.entrySet()) { - if (!isReadOnly(entry.getKey())) { - setHeader(entry.getKey(), entry.getValue()); + headersToCopy.forEach((key, value) -> { + if (!isReadOnly(key)) { + setHeader(key, value); } - } + }); } } @@ -401,11 +401,11 @@ public class MessageHeaderAccessor { */ public void copyHeadersIfAbsent(@Nullable Map headersToCopy) { if (headersToCopy != null) { - for (Map.Entry entry : headersToCopy.entrySet()) { - if (!isReadOnly(entry.getKey())) { - setHeaderIfAbsent(entry.getKey(), entry.getValue()); + headersToCopy.forEach((key, value) -> { + if (!isReadOnly(key)) { + setHeaderIfAbsent(key, value); } - } + }); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java index a068fa0ef4..b82f7138fe 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java @@ -197,11 +197,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor { if (headers == null) { return; } - for (Map.Entry> headerEntry : headers.entrySet()) { - for (String value : headerEntry.getValue()) { - addNativeHeader(headerEntry.getKey(), value); - } - } + headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value))); } @Nullable diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java index dc5bf08f15..69117cdd22 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java @@ -325,11 +325,11 @@ public abstract class AbstractEntityManagerFactoryBean implements } Map vendorPropertyMap = this.jpaVendorAdapter.getJpaPropertyMap(); if (vendorPropertyMap != null) { - for (Map.Entry entry : vendorPropertyMap.entrySet()) { - if (!this.jpaPropertyMap.containsKey(entry.getKey())) { - this.jpaPropertyMap.put(entry.getKey(), entry.getValue()); + vendorPropertyMap.forEach((key, value) -> { + if (!this.jpaPropertyMap.containsKey(key)) { + this.jpaPropertyMap.put(key, value); } - } + }); } if (this.entityManagerFactoryInterface == null) { this.entityManagerFactoryInterface = this.jpaVendorAdapter.getEntityManagerFactoryInterface(); diff --git a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index d2848bd1ed..3e6f66c03c 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -474,9 +474,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing context.addPackages(targetPackages); } if (this.castorProperties != null) { - for (Map.Entry property : this.castorProperties.entrySet()) { - context.setProperty(property.getKey(), property.getValue()); - } + this.castorProperties.forEach(context::setProperty); } return context; } @@ -563,19 +561,13 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing marshaller.setSchemaLocation(this.schemaLocation); marshaller.setUseXSITypeAtRoot(this.useXSITypeAtRoot); if (this.doctypes != null) { - for (Map.Entry doctype : this.doctypes.entrySet()) { - marshaller.setDoctype(doctype.getKey(), doctype.getValue()); - } + this.doctypes.forEach(marshaller::setDoctype); } if (this.processingInstructions != null) { - for (Map.Entry processingInstruction : this.processingInstructions.entrySet()) { - marshaller.addProcessingInstruction(processingInstruction.getKey(), processingInstruction.getValue()); - } + this.processingInstructions.forEach(marshaller::addProcessingInstruction); } if (this.namespaceMappings != null) { - for (Map.Entry entry : this.namespaceMappings.entrySet()) { - marshaller.setNamespaceMapping(entry.getKey(), entry.getValue()); - } + this.namespaceMappings.forEach(marshaller::setNamespaceMapping); } } @@ -666,9 +658,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing unmarshaller.setReuseObjects(this.reuseObjects); unmarshaller.setClearCollections(this.clearCollections); if (this.namespaceToPackageMapping != null) { - for (Map.Entry mapping : this.namespaceToPackageMapping.entrySet()) { - unmarshaller.addNamespaceToPackageMapping(mapping.getKey(), mapping.getValue()); - } + this.namespaceToPackageMapping.forEach(unmarshaller::addNamespaceToPackageMapping); } if (this.entityResolver != null) { unmarshaller.setEntityResolver(this.entityResolver); diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java index b2fb29717a..8e3531684d 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -469,15 +469,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo try { if (this.aliases != null) { Map> classMap = toClassMap(this.aliases); - for (Map.Entry> entry : classMap.entrySet()) { - xstream.alias(entry.getKey(), entry.getValue()); - } + classMap.forEach(xstream::alias); } if (this.aliasesByType != null) { Map> classMap = toClassMap(this.aliasesByType); - for (Map.Entry> entry : classMap.entrySet()) { - xstream.aliasType(entry.getKey(), entry.getValue()); - } + classMap.forEach(xstream::aliasType); } if (this.fieldAliases != null) { for (Map.Entry entry : this.fieldAliases.entrySet()) { @@ -543,20 +539,20 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo } if (this.implicitCollections != null) { - for (Map.Entry, String> entry : this.implicitCollections.entrySet()) { - String[] collectionFields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); + this.implicitCollections.forEach((key, fields) -> { + String[] collectionFields = StringUtils.commaDelimitedListToStringArray(fields); for (String collectionField : collectionFields) { - xstream.addImplicitCollection(entry.getKey(), collectionField); + xstream.addImplicitCollection(key, collectionField); } - } + }); } if (this.omittedFields != null) { - for (Map.Entry, String> entry : this.omittedFields.entrySet()) { - String[] fields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); + this.omittedFields.forEach((key, value) -> { + String[] fields = StringUtils.commaDelimitedListToStringArray(value); for (String field : fields) { - xstream.omitField(entry.getKey(), field); + xstream.omitField(key, field); } - } + }); } if (this.annotatedClasses != null) { diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index 5ea6305b29..52a796d1b6 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java @@ -26,7 +26,6 @@ import java.util.Enumeration; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.StringTokenizer; import javax.servlet.ServletContext; @@ -182,11 +181,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { // parameter Map parentParams = parentRequest.getParameterMap(); - for (Map.Entry parentParam : parentParams.entrySet()) { - String paramName = parentParam.getKey(); - String[] paramValues = parentParam.getValue(); - request.addParameter(paramName, paramValues); - } + parentParams.forEach(request::addParameter); // cookie Cookie[] parentCookies = parentRequest.getCookies(); @@ -314,9 +309,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private void headers(MockHttpServletRequest request) { - for (Entry header : this.webRequest.getAdditionalHeaders().entrySet()) { - request.addHeader(header.getKey(), header.getValue()); - } + this.webRequest.getAdditionalHeaders().forEach(request::addHeader); } private MockHttpSession httpSession(MockHttpServletRequest request, final String sessionid) { @@ -359,14 +352,13 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private void params(MockHttpServletRequest request, UriComponents uriComponents) { - for (Entry> entry : uriComponents.getQueryParams().entrySet()) { - String name = entry.getKey(); + uriComponents.getQueryParams().forEach((name, values) -> { String urlDecodedName = urlDecode(name); - for (String value : entry.getValue()) { + values.forEach(value -> { value = (value != null ? urlDecode(value) : ""); request.addParameter(urlDecodedName, value); - } - } + }); + }); for (NameValuePair param : this.webRequest.getRequestParameters()) { request.addParameter(param.getName(), param.getValue()); } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index 2127127538..d4a9d3d78a 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -28,7 +28,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Map.Entry; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.http.Cookie; @@ -310,9 +309,7 @@ public class MockHttpServletRequestBuilder * @param httpHeaders the headers and values to add */ public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) { - for (Map.Entry> entry : httpHeaders.entrySet()) { - this.headers.addAll(entry.getKey(), entry.getValue()); - } + httpHeaders.forEach(this.headers::addAll); return this; } @@ -697,12 +694,10 @@ public class MockHttpServletRequestBuilder } private void addRequestParams(MockHttpServletRequest request, MultiValueMap map) { - for (Entry> entry : map.entrySet()) { - for (String value : entry.getValue()) { - value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null); - request.addParameter(UriUtils.decode(entry.getKey(), StandardCharsets.UTF_8), value); - } - } + map.forEach((key, values) -> values.forEach(value -> { + value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null); + request.addParameter(UriUtils.decode(key, StandardCharsets.UTF_8), value); + })); } private MultiValueMap parseFormData(final MediaType mediaType) { diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/MethodMapTransactionAttributeSource.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/MethodMapTransactionAttributeSource.java index a994c8b48c..d78ef28d94 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/MethodMapTransactionAttributeSource.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/MethodMapTransactionAttributeSource.java @@ -106,9 +106,7 @@ public class MethodMapTransactionAttributeSource */ protected void initMethodMap(@Nullable Map methodMap) { if (methodMap != null) { - for (Map.Entry entry : methodMap.entrySet()) { - addTransactionalMethod(entry.getKey(), entry.getValue()); - } + methodMap.forEach(this::addTransactionalMethod); } } diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java index 2efd477e24..3e55ee62ed 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java @@ -61,9 +61,7 @@ public class NameMatchTransactionAttributeSource implements TransactionAttribute * @see TransactionAttributeEditor */ public void setNameMap(Map nameMap) { - for (Map.Entry entry : nameMap.entrySet()) { - addTransactionalMethod(entry.getKey(), entry.getValue()); - } + nameMap.forEach(this::addTransactionalMethod); } /** From 1ab678a2a3a2fe7a1977600178d1b9776f857870 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 13 Jun 2017 15:09:09 +0200 Subject: [PATCH 2/2] Polish "Refactor iterator of Map with Java8's Map.forEach" Closes gh-1459 --- .../cache/caffeine/CaffeineCacheManager.java | 7 +++++-- .../jdbc/core/namedparam/MapSqlParameterSource.java | 2 +- .../annotation/JmsListenerAnnotationBeanPostProcessor.java | 3 ++- .../messaging/support/NativeMessageHeaderAccessor.java | 3 ++- .../org/springframework/oxm/castor/CastorMarshaller.java | 2 +- .../interceptor/NameMatchTransactionAttributeSource.java | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index 16b616e5c1..144ef388c7 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -19,6 +19,7 @@ package org.springframework.cache.caffeine; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -222,7 +223,9 @@ public class CaffeineCacheManager implements CacheManager { * Create the known caches again with the current state of this manager. */ private void refreshKnownCaches() { - this.cacheMap.forEach((key, value) -> createCaffeineCache(key)); + for (Map.Entry entry : this.cacheMap.entrySet()) { + entry.setValue(createCaffeineCache(entry.getKey())); + } } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java index 886345f40f..a411557bc8 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. diff --git a/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java b/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java index cdec528d39..0ca4e8c63d 100644 --- a/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java +++ b/spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java @@ -226,7 +226,8 @@ public class JmsListenerAnnotationBeanPostProcessor else { // Non-empty set of methods annotatedMethods.forEach((method, listeners) -> - listeners.forEach(listener -> processJmsListener(listener, method, bean))); + listeners.forEach(listener -> + processJmsListener(listener, method, bean))); if (logger.isDebugEnabled()) { logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName + "': " + annotatedMethods); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java index b82f7138fe..3049f07c15 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java @@ -197,7 +197,8 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor { if (headers == null) { return; } - headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value))); + headers.forEach((key, values) -> + values.forEach(value -> addNativeHeader(key, value))); } @Nullable diff --git a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index 3e6f66c03c..9aed389b63 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java index 3e55ee62ed..9edc18d400 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/NameMatchTransactionAttributeSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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.