Skip to content

Commit aca5eb7

Browse files
committed
Merge pull request #1449 from diguage:dev
* pr/1449: Polish "Use Map#forEach instead of Map#entrySet#forEach" Use Map#forEach instead of Map#entrySet#forEach
2 parents 424bc75 + 451b419 commit aca5eb7

File tree

9 files changed

+29
-38
lines changed

9 files changed

+29
-38
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,9 @@ public static <T> Map<String, T> beansOfTypeIncludingAncestors(ListableBeanFacto
265265
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
266266
Map<String, T> parentResult = beansOfTypeIncludingAncestors(
267267
(ListableBeanFactory) hbf.getParentBeanFactory(), type);
268-
parentResult.entrySet().forEach(entry -> {
269-
String beanName = entry.getKey();
268+
parentResult.forEach((beanName, beanType) -> {
270269
if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
271-
result.put(beanName, entry.getValue());
270+
result.put(beanName, beanType);
272271
}
273272
});
274273
}
@@ -314,11 +313,9 @@ public static <T> Map<String, T> beansOfTypeIncludingAncestors(
314313
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
315314
Map<String, T> parentResult = beansOfTypeIncludingAncestors(
316315
(ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit);
317-
318-
parentResult.entrySet().forEach(entry -> {
319-
String beanName = entry.getKey();
316+
parentResult.forEach((beanName, beanType) -> {
320317
if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
321-
result.put(beanName, entry.getValue());
318+
result.put(beanName, beanType);
322319
}
323320
});
324321
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,8 @@ protected void registerCustomEditors(PropertyEditorRegistry registry) {
11871187
}
11881188
}
11891189
if (!this.customEditors.isEmpty()) {
1190-
this.customEditors.entrySet().forEach(entry -> {
1191-
Class<?> requiredType = entry.getKey();
1192-
Class<? extends PropertyEditor> editorClass = entry.getValue();
1193-
registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
1194-
});
1190+
this.customEditors.forEach((requiredType, editorClass) ->
1191+
registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass)));
11951192
}
11961193
}
11971194

spring-context/src/main/java/org/springframework/validation/support/BindingAwareConcurrentModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Object put(String key, Object value) {
4848

4949
@Override
5050
public void putAll(Map<? extends String, ?> map) {
51-
map.entrySet().forEach(e -> removeBindingResultIfNecessary(e.getKey(), e.getValue()));
51+
map.forEach(this::removeBindingResultIfNecessary);
5252
super.putAll(map);
5353
}
5454

spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ public Set<Entry<K, List<V>>> entrySet() {
195195
*/
196196
public LinkedMultiValueMap<K, V> deepCopy() {
197197
LinkedMultiValueMap<K, V> copy = new LinkedMultiValueMap<>(this.targetMap.size());
198-
this.targetMap.entrySet().forEach(entry ->
199-
copy.put(entry.getKey(), new LinkedList<>(entry.getValue())));
198+
this.targetMap.forEach((k, v) -> copy.put(k, new LinkedList<>(v)));
200199

201200
return copy;
202201
}

spring-web/src/main/java/org/springframework/web/util/UriUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ public static String encode(String source, Charset charset) {
194194
*/
195195
public static Map<String, String> encodeUriVariables(Map<String, ?> uriVariables) {
196196
Map<String, String> result = new LinkedHashMap<>(uriVariables.size());
197-
uriVariables.entrySet().stream().forEach(entry -> {
198-
String stringValue = (entry.getValue() != null ? entry.getValue().toString() : "");
199-
result.put(entry.getKey(), encode(stringValue, StandardCharsets.UTF_8));
197+
uriVariables.forEach((key, value) -> {
198+
String stringValue = (value != null ? value.toString() : "");
199+
result.put(key, encode(stringValue, StandardCharsets.UTF_8));
200200
});
201201
return result;
202202
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ public List<SyncInvocableHandlerMethod> getInitBinderMethods(HandlerMethod handl
224224
Class<?> handlerType = handlerMethod.getBeanType();
225225

226226
// Global methods first
227-
this.initBinderAdviceCache.entrySet().forEach(entry -> {
228-
if (entry.getKey().isApplicableToBeanType(handlerType)) {
229-
Object bean = entry.getKey().resolveBean();
230-
entry.getValue().forEach(method -> result.add(getInitBinderMethod(bean, method)));
227+
this.initBinderAdviceCache.forEach((adviceBean, methods) -> {
228+
if (adviceBean.isApplicableToBeanType(handlerType)) {
229+
Object bean = adviceBean.resolveBean();
230+
methods.forEach(method -> result.add(getInitBinderMethod(bean, method)));
231231
}
232232
});
233233

@@ -257,10 +257,10 @@ public List<InvocableHandlerMethod> getModelAttributeMethods(HandlerMethod handl
257257
Class<?> handlerType = handlerMethod.getBeanType();
258258

259259
// Global methods first
260-
this.modelAttributeAdviceCache.entrySet().forEach(entry -> {
261-
if (entry.getKey().isApplicableToBeanType(handlerType)) {
262-
Object bean = entry.getKey().resolveBean();
263-
entry.getValue().forEach(method -> result.add(createAttributeMethod(bean, method)));
260+
this.modelAttributeAdviceCache.forEach((adviceBean, methods) -> {
261+
if (adviceBean.isApplicableToBeanType(handlerType)) {
262+
Object bean = adviceBean.resolveBean();
263+
methods.forEach(method -> result.add(createAttributeMethod(bean, method)));
264264
}
265265
});
266266

spring-webflux/src/main/java/org/springframework/web/reactive/socket/client/ReactorNettyWebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public Mono<Void> execute(URI url, HttpHeaders headers, WebSocketHandler handler
9393
}
9494

9595
private void setNettyHeaders(HttpHeaders headers, io.netty.handler.codec.http.HttpHeaders nettyHeaders) {
96-
headers.keySet().stream().forEach(key -> nettyHeaders.set(key, headers.get(key)));
96+
headers.forEach(nettyHeaders::set);
9797
}
9898

9999
private HttpHeaders toHttpHeaders(HttpClientResponse response) {

spring-webmvc/src/test/java/org/springframework/web/servlet/view/BaseViewTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -281,9 +281,8 @@ public void attributeCSVParsingIgoresTrailingComma() {
281281
*/
282282
@SuppressWarnings({ "rawtypes", "unchecked" })
283283
private void checkContainsAll(Map expected, Map<String, Object> actual) {
284-
expected.keySet().stream().forEach(
285-
key -> assertEquals("Values for model key '" + key + "' must match", expected.get(key), actual.get(key))
286-
);
284+
expected.forEach((k, v) -> assertEquals("Values for model key '" + k
285+
+ "' must match", expected.get(k), actual.get(k)));
287286
}
288287

289288

spring-webmvc/src/test/java/org/springframework/web/servlet/view/InternalResourceViewTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,9 +84,8 @@ public int getMinorVersion() {
8484
view.render(model, request, response);
8585
assertEquals(url, response.getForwardedUrl());
8686

87-
model.keySet().stream().forEach(
88-
key -> assertEquals("Values for model key '" + key + "' must match", model.get(key), request.getAttribute(key))
89-
);
87+
model.forEach((key, value) -> assertEquals("Values for model key '" + key
88+
+ "' must match", value, request.getAttribute(key)));
9089
}
9190

9291
@Test
@@ -101,7 +100,7 @@ public void alwaysInclude() throws Exception {
101100
view.render(model, request, response);
102101
assertEquals(url, response.getIncludedUrl());
103102

104-
model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
103+
model.forEach((key, value) -> verify(request).setAttribute(key, value));
105104
}
106105

107106
@Test
@@ -116,7 +115,7 @@ public void includeOnAttribute() throws Exception {
116115
view.render(model, request, response);
117116
assertEquals(url, response.getIncludedUrl());
118117

119-
model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
118+
model.forEach((key, value) -> verify(request).setAttribute(key, value));
120119
}
121120

122121
@Test
@@ -132,7 +131,7 @@ public void includeOnCommitted() throws Exception {
132131
view.render(model, request, response);
133132
assertEquals(url, response.getIncludedUrl());
134133

135-
model.keySet().stream().forEach(key -> verify(request).setAttribute(key, model.get(key)));
134+
model.forEach((k, v) -> verify(request).setAttribute(k, v));
136135
}
137136

138137
}

0 commit comments

Comments
 (0)