Skip to content

Commit 1ef5f61

Browse files
diguagesnicoll
authored andcommitted
Refactor iterator of Map with Java8's Map.forEach
See gh-1459
1 parent 7018804 commit 1ef5f61

File tree

18 files changed

+72
-130
lines changed

18 files changed

+72
-130
lines changed

spring-context-indexer/src/main/java/org/springframework/context/index/PropertiesMarshaller.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.OutputStream;
2222
import java.util.Arrays;
2323
import java.util.HashSet;
24-
import java.util.Map;
2524
import java.util.Properties;
2625
import java.util.Set;
2726

@@ -43,11 +42,10 @@ public static CandidateComponentsMetadata read(InputStream in) throws IOExceptio
4342
CandidateComponentsMetadata result = new CandidateComponentsMetadata();
4443
Properties props = new Properties();
4544
props.load(in);
46-
for (Map.Entry<Object, Object> entry : props.entrySet()) {
47-
String type = (String) entry.getKey();
48-
Set<String> candidates = new HashSet<>(Arrays.asList(((String) entry.getValue()).split(",")));
49-
result.add(new ItemMetadata(type, candidates));
50-
}
45+
props.forEach((type, value) -> {
46+
Set<String> candidates = new HashSet<>(Arrays.asList(((String) value).split(",")));
47+
result.add(new ItemMetadata((String) type, candidates));
48+
});
5149
return result;
5250
}
5351

spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Arrays;
2020
import java.util.Collection;
2121
import java.util.Collections;
22-
import java.util.Map;
2322
import java.util.concurrent.ConcurrentHashMap;
2423
import java.util.concurrent.ConcurrentMap;
2524

@@ -223,9 +222,7 @@ private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder) {
223222
* Create the known caches again with the current state of this manager.
224223
*/
225224
private void refreshKnownCaches() {
226-
for (Map.Entry<String, Cache> entry : this.cacheMap.entrySet()) {
227-
entry.setValue(createCaffeineCache(entry.getKey()));
228-
}
225+
this.cacheMap.forEach((key, value) -> createCaffeineCache(key));
229226
}
230227

231228
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,12 @@ public MapSqlParameterSource addValue(String paramName, Object value, int sqlTyp
131131
*/
132132
public MapSqlParameterSource addValues(@Nullable Map<String, ?> values) {
133133
if (values != null) {
134-
for (Map.Entry<String, ?> entry : values.entrySet()) {
135-
this.values.put(entry.getKey(), entry.getValue());
136-
if (entry.getValue() instanceof SqlParameterValue) {
137-
SqlParameterValue value = (SqlParameterValue) entry.getValue();
138-
registerSqlType(entry.getKey(), value.getSqlType());
134+
values.forEach((key, value) -> {
135+
this.values.put(key, value);
136+
if (value instanceof SqlParameterValue) {
137+
registerSqlType(key, ((SqlParameterValue) value).getSqlType());
139138
}
140-
}
139+
});
141140
}
142141
return this;
143142
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public void afterPropertiesSet() {
114114
throw new IllegalArgumentException("Property 'targetDataSources' is required");
115115
}
116116
this.resolvedDataSources = new HashMap<>(this.targetDataSources.size());
117-
for (Map.Entry<Object, Object> entry : this.targetDataSources.entrySet()) {
118-
Object lookupKey = resolveSpecifiedLookupKey(entry.getKey());
119-
DataSource dataSource = resolveSpecifiedDataSource(entry.getValue());
117+
this.targetDataSources.forEach((key, value) -> {
118+
Object lookupKey = resolveSpecifiedLookupKey(key);
119+
DataSource dataSource = resolveSpecifiedDataSource(value);
120120
this.resolvedDataSources.put(lookupKey, dataSource);
121-
}
121+
});
122122
if (this.defaultTargetDataSource != null) {
123123
this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
124124
}

spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,8 @@ public Object postProcessAfterInitialization(final Object bean, String beanName)
225225
}
226226
else {
227227
// Non-empty set of methods
228-
for (Map.Entry<Method, Set<JmsListener>> entry : annotatedMethods.entrySet()) {
229-
Method method = entry.getKey();
230-
for (JmsListener listener : entry.getValue()) {
231-
processJmsListener(listener, method, bean);
232-
}
233-
}
228+
annotatedMethods.forEach((method, listeners) ->
229+
listeners.forEach(listener -> processJmsListener(listener, method, bean)));
234230
if (logger.isDebugEnabled()) {
235231
logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName +
236232
"': " + annotatedMethods);

spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,10 @@ public void setTypeIdPropertyName(String typeIdPropertyName) {
158158
*/
159159
public void setTypeIdMappings(Map<String, Class<?>> typeIdMappings) {
160160
this.idClassMappings = new HashMap<>();
161-
for (Map.Entry<String, Class<?>> entry : typeIdMappings.entrySet()) {
162-
String id = entry.getKey();
163-
Class<?> clazz = entry.getValue();
161+
typeIdMappings.forEach((id, clazz) -> {
164162
this.idClassMappings.put(id, clazz);
165163
this.classIdMappings.put(clazz, id);
166-
}
164+
});
167165
}
168166

169167
@Override

spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ else if (timestamp < 0) {
149149
*/
150150
private MessageHeaders(MessageHeaders original, Set<String> keysToIgnore) {
151151
this.headers = new HashMap<>(original.headers.size() - keysToIgnore.size());
152-
for (Map.Entry<String, Object> entry : original.headers.entrySet()) {
153-
if (!keysToIgnore.contains(entry.getKey())) {
154-
this.headers.put(entry.getKey(), entry.getValue());
152+
original.headers.forEach((key, value) -> {
153+
if (!keysToIgnore.contains(key)) {
154+
this.headers.put(key, value);
155155
}
156-
}
156+
});
157157
}
158158

159159

@@ -276,11 +276,11 @@ public void clear() {
276276

277277
private void writeObject(ObjectOutputStream out) throws IOException {
278278
Set<String> keysToIgnore = new HashSet<>();
279-
for (Map.Entry<String, Object> entry : this.headers.entrySet()) {
280-
if (!(entry.getValue() instanceof Serializable)) {
281-
keysToIgnore.add(entry.getKey());
279+
this.headers.forEach((key, value) -> {
280+
if (!(value instanceof Serializable)) {
281+
keysToIgnore.add(key);
282282
}
283-
}
283+
});
284284

285285
if (keysToIgnore.isEmpty()) {
286286
// All entries are serializable -> serialize the regular MessageHeaders instance

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,7 @@ protected final void detectHandlerMethods(final Object handler) {
288288
if (logger.isDebugEnabled()) {
289289
logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
290290
}
291-
for (Map.Entry<Method, T> entry : methods.entrySet()) {
292-
registerHandlerMethod(handler, entry.getKey(), entry.getValue());
293-
}
291+
methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
294292
}
295293
}
296294

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ private StompHeaders(Map<String, List<String>> headers, boolean readOnly) {
113113
Assert.notNull(headers, "'headers' must not be null");
114114
if (readOnly) {
115115
Map<String, List<String>> map = new LinkedMultiValueMap<>(headers.size());
116-
for (Entry<String, List<String>> entry : headers.entrySet()) {
117-
List<String> values = Collections.unmodifiableList(entry.getValue());
118-
map.put(entry.getKey(), values);
119-
}
116+
headers.forEach((key, value) -> map.put(key, Collections.unmodifiableList(value)));
120117
this.headers = Collections.unmodifiableMap(map);
121118
}
122119
else {
@@ -424,9 +421,7 @@ public void addAll(String headerName, List<? extends String> headerValues) {
424421

425422
@Override
426423
public void addAll(MultiValueMap<String, String> values) {
427-
for (Entry<String, List<String>> entry : values.entrySet()) {
428-
addAll(entry.getKey(), entry.getValue());
429-
}
424+
values.forEach(this::addAll);
430425
}
431426

432427
/**
@@ -446,17 +441,13 @@ public void set(String headerName, String headerValue) {
446441

447442
@Override
448443
public void setAll(Map<String, String> values) {
449-
for (Entry<String, String> entry : values.entrySet()) {
450-
set(entry.getKey(), entry.getValue());
451-
}
444+
values.forEach(this::set);
452445
}
453446

454447
@Override
455448
public Map<String, String> toSingleValueMap() {
456449
LinkedHashMap<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
457-
for (Entry<String, List<String>> entry : headers.entrySet()) {
458-
singleValueMap.put(entry.getKey(), entry.getValue().get(0));
459-
}
450+
headers.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
460451
return singleValueMap;
461452
}
462453

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ private List<String> getMatchingHeaderNames(String pattern, @Nullable Map<String
387387
*/
388388
public void copyHeaders(@Nullable Map<String, ?> headersToCopy) {
389389
if (headersToCopy != null) {
390-
for (Map.Entry<String, ?> entry : headersToCopy.entrySet()) {
391-
if (!isReadOnly(entry.getKey())) {
392-
setHeader(entry.getKey(), entry.getValue());
390+
headersToCopy.forEach((key, value) -> {
391+
if (!isReadOnly(key)) {
392+
setHeader(key, value);
393393
}
394-
}
394+
});
395395
}
396396
}
397397

@@ -401,11 +401,11 @@ public void copyHeaders(@Nullable Map<String, ?> headersToCopy) {
401401
*/
402402
public void copyHeadersIfAbsent(@Nullable Map<String, ?> headersToCopy) {
403403
if (headersToCopy != null) {
404-
for (Map.Entry<String, ?> entry : headersToCopy.entrySet()) {
405-
if (!isReadOnly(entry.getKey())) {
406-
setHeaderIfAbsent(entry.getKey(), entry.getValue());
404+
headersToCopy.forEach((key, value) -> {
405+
if (!isReadOnly(key)) {
406+
setHeaderIfAbsent(key, value);
407407
}
408-
}
408+
});
409409
}
410410
}
411411

0 commit comments

Comments
 (0)