Skip to content

Commit 93ef795

Browse files
committed
More use entrySet() rather than using keySet() and get(key)
This commit replaces some more occurrances of keySet() and get(key) with more efficient usage of the map's entry set. See spring-projectsgh-4813
1 parent 8f1f8dd commit 93ef795

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -36,6 +36,7 @@
3636
* {@link Endpoint} to expose Spring MVC mappings.
3737
*
3838
* @author Dave Syer
39+
* @author Andy Wilkinson
3940
*/
4041
@ConfigurationProperties(prefix = "endpoints.mappings", ignoreUnknownFields = false)
4142
public class RequestMappingEndpoint extends AbstractEndpoint<Map<String, Object>>
@@ -84,20 +85,19 @@ public Map<String, Object> invoke() {
8485
return result;
8586
}
8687

88+
@SuppressWarnings("rawtypes")
8789
protected void extractMethodMappings(ApplicationContext applicationContext,
8890
Map<String, Object> result) {
8991
if (applicationContext != null) {
90-
for (String name : applicationContext
91-
.getBeansOfType(AbstractHandlerMethodMapping.class).keySet()) {
92+
for (Entry<String, AbstractHandlerMethodMapping> bean : applicationContext
93+
.getBeansOfType(AbstractHandlerMethodMapping.class).entrySet()) {
9294
@SuppressWarnings("unchecked")
93-
Map<?, HandlerMethod> methods = applicationContext
94-
.getBean(name, AbstractHandlerMethodMapping.class)
95-
.getHandlerMethods();
95+
Map<?, HandlerMethod> methods = bean.getValue().getHandlerMethods();
9696
for (Entry<?, HandlerMethod> method : methods.entrySet()) {
9797
Map<String, String> map = new LinkedHashMap<String, String>();
98-
map.put("bean", name);
99-
map.put("method", method.getValue().toString());
100-
result.put( method.getKey().toString(), map);
98+
map.put("bean", bean.getKey());
99+
map.put("method", method.getValue().toString());
100+
result.put(method.getKey().toString(), map);
101101
}
102102
}
103103
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -23,11 +23,15 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Map.Entry;
27+
import java.util.SortedSet;
28+
import java.util.TreeSet;
2629

2730
/**
2831
* A helper class generating a report from the meta-data of a particular service.
2932
*
3033
* @author Stephane Nicoll
34+
* @author Andy Wilkinson
3135
* @since 1.2.0
3236
*/
3337
class ServiceCapabilitiesReportGenerator {
@@ -104,11 +108,20 @@ private void reportAvailableProjectTypes(InitializrServiceMetadata metadata,
104108
StringBuilder report) {
105109
report.append("Available project types:" + NEW_LINE);
106110
report.append("------------------------" + NEW_LINE);
107-
List<String> typeIds = new ArrayList<String>(metadata.getProjectTypes().keySet());
108-
Collections.sort(typeIds);
109-
for (String typeId : typeIds) {
110-
ProjectType type = metadata.getProjectTypes().get(typeId);
111-
report.append(typeId + " - " + type.getName());
111+
SortedSet<Entry<String, ProjectType>> entries = new TreeSet<Entry<String, ProjectType>>(
112+
new Comparator<Entry<String, ProjectType>>() {
113+
114+
@Override
115+
public int compare(Entry<String, ProjectType> o1,
116+
Entry<String, ProjectType> o2) {
117+
return o1.getKey().compareTo(o2.getKey());
118+
}
119+
120+
});
121+
entries.addAll(metadata.getProjectTypes().entrySet());
122+
for (Entry<String, ProjectType> entry : entries) {
123+
ProjectType type = entry.getValue();
124+
report.append(entry.getKey() + " - " + type.getName());
112125
if (!type.getTags().isEmpty()) {
113126
reportTags(report, type);
114127
}

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/PropertiesMergingResourceTransformer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.util.List;
22+
import java.util.Map.Entry;
2223
import java.util.Properties;
2324
import java.util.jar.JarEntry;
2425
import java.util.jar.JarOutputStream;
@@ -32,6 +33,7 @@
3233
* to be merged without losing any information.
3334
*
3435
* @author Dave Syer
36+
* @author Andy Wilkinson
3537
*/
3638
public class PropertiesMergingResourceTransformer implements ResourceTransformer {
3739

@@ -62,9 +64,9 @@ public void processResource(String resource, InputStream is,
6264
Properties properties = new Properties();
6365
properties.load(is);
6466
is.close();
65-
for (Object key : properties.keySet()) {
66-
String name = (String) key;
67-
String value = properties.getProperty(name);
67+
for (Entry<Object, Object> entry : properties.entrySet()) {
68+
String name = (String) entry.getKey();
69+
String value = (String) entry.getValue();
6870
String existing = this.data.getProperty(name);
6971
this.data.setProperty(name,
7072
existing == null ? value : existing + "," + value);

0 commit comments

Comments
 (0)