From 1b9e12f52fc1bd032c237b562afe6901fc9da9a0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 28 Apr 2017 10:53:23 +0200 Subject: [PATCH] Polish "Use Java 8 forEach method on Map" Closes gh-1404 --- .../beans/factory/BeanFactoryUtils.java | 4 ++-- .../main/java/org/springframework/util/ClassUtils.java | 8 ++++---- .../java/org/springframework/util/CollectionUtils.java | 10 +++++----- .../springframework/util/LinkedCaseInsensitiveMap.java | 3 +-- .../org/springframework/util/LinkedMultiValueMap.java | 5 ++--- .../result/method/AbstractHandlerMethodMapping.java | 6 ++---- .../web/servlet/handler/SimpleUrlHandlerMapping.java | 6 ++---- 7 files changed, 18 insertions(+), 24 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java index 61b7791a2b..39bdea9c0d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.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. @@ -268,7 +268,7 @@ public abstract class BeanFactoryUtils { String beanName = entry.getKey(); if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) { result.put(beanName, entry.getValue()); - } + } }); } } diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 3a33c103d5..de8124db35 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -106,10 +106,10 @@ public abstract class ClassUtils { primitiveWrapperTypeMap.put(Integer.class, int.class); primitiveWrapperTypeMap.put(Long.class, long.class); primitiveWrapperTypeMap.put(Short.class, short.class); - - primitiveWrapperTypeMap.entrySet().forEach(entry -> { - primitiveTypeToWrapperMap.put(entry.getValue(), entry.getKey()); - registerCommonClasses(entry.getKey()); + + primitiveWrapperTypeMap.forEach((key, value) -> { + primitiveTypeToWrapperMap.put(value, key); + registerCommonClasses(key); }); Set> primitiveTypes = new HashSet<>(32); diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 572d1348dd..a2d107af7a 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -354,9 +354,9 @@ public abstract class CollectionUtils { public static MultiValueMap unmodifiableMultiValueMap(MultiValueMap map) { Assert.notNull(map, "'map' must not be null"); Map> result = new LinkedHashMap<>(map.size()); - map.entrySet().forEach(entry -> { - List values = Collections.unmodifiableList(entry.getValue()); - result.put(entry.getKey(), (List) values); + map.forEach((key, value) -> { + List values = Collections.unmodifiableList(value); + result.put(key, (List) values); }); Map> unmodifiableMap = Collections.unmodifiableMap(result); return toMultiValueMap(unmodifiableMap); @@ -431,13 +431,13 @@ public abstract class CollectionUtils { @Override public void setAll(Map values) { - values.entrySet().forEach(entry -> set(entry.getKey(), entry.getValue())); + values.forEach(this::set); } @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap<>(this.map.size()); - map.entrySet().forEach(entry -> singleValueMap.put(entry.getKey(), entry.getValue().get(0))); + this.map.forEach((key, value) -> singleValueMap.put(key, value.get(0))); return singleValueMap; } diff --git a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java index 149ca4f83e..268fc11259 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java @@ -169,8 +169,7 @@ public class LinkedCaseInsensitiveMap implements Map, Serializable if (map.isEmpty()) { return; } - map.entrySet().forEach(entry -> put(entry.getKey(), entry.getValue())); - + map.forEach(this::put); } @Override diff --git a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java index 252fd35189..7c48ad2204 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java @@ -100,14 +100,13 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa @Override public void setAll(Map values) { - values.entrySet().forEach(entry -> set(entry.getKey(), entry.getValue())); + values.forEach(this::set); } @Override public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap<>(this.targetMap.size()); - this.targetMap.entrySet().forEach(entry -> - singleValueMap.put(entry.getKey(), entry.getValue().get(0))); + this.targetMap.forEach((key, value) -> singleValueMap.put(key, value.get(0))); return singleValueMap; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java index 3812a94f8d..585bd12571 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java @@ -192,10 +192,8 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap if (logger.isDebugEnabled()) { logger.debug(methods.size() + " request handler methods found on " + userType + ": " + methods); } - - methods.entrySet().forEach(entry -> { - Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType); - T mapping = entry.getValue(); + methods.forEach((key, mapping) -> { + Method invocableMethod = AopUtils.selectInvocableMethod(key, userType); registerHandlerMethod(handler, invocableMethod, mapping); }); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java index 0542f3d50f..02b69ed6a0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.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. @@ -114,9 +114,7 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping { logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping"); } else { - urlMap.entrySet().forEach(entry -> { - String url = entry.getKey(); - Object handler = entry.getValue(); + urlMap.forEach((url, handler) -> { // Prepend with slash if not already present. if (!url.startsWith("/")) { url = "/" + url;