From 84378da1bb954a649bcf783de7864ff87de876ba Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Sun, 5 Jan 2025 13:30:06 +0200 Subject: [PATCH] Use Map.forEach for iterating over values (#1280) For maps, forEach is more efficient as it allows the traversal of the internal structure with minimal overhead compares to use entries --- .../io/smallrye/config/EnvConfigSource.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java b/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java index b701a5035..bbd03bf42 100644 --- a/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java @@ -29,6 +29,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.function.BiConsumer; import io.smallrye.config.common.AbstractConfigSource; @@ -78,14 +79,16 @@ public EnvConfigSource(final Map properties, final int ordinal) @Override public Map getProperties() { Map properties = new HashMap<>(); - for (Map.Entry entry : envVars.getEnv().entrySet()) { - EnvEntry entryValue = entry.getValue(); - if (entryValue.getEntries() != null) { - properties.putAll(entryValue.getEntries()); - } else { - properties.put(entryValue.getName(), entryValue.getValue()); + envVars.getEnv().forEach(new BiConsumer<>() { + @Override + public void accept(EnvName key, EnvEntry entryValue) { + if (entryValue.getEntries() != null) { + properties.putAll(entryValue.getEntries()); + } else { + properties.put(entryValue.getName(), entryValue.getValue()); + } } - } + }); return properties; } @@ -149,17 +152,20 @@ static final class EnvVars implements Serializable { public EnvVars(final Map properties) { this.env = new HashMap<>(properties.size()); this.names = new HashSet<>(properties.size() * 2); - for (Map.Entry entry : properties.entrySet()) { - EnvName envName = new EnvName(entry.getKey()); - EnvEntry envEntry = env.get(envName); - if (envEntry == null) { - env.put(envName, new EnvEntry(entry.getKey(), entry.getValue())); - } else { - envEntry.add(entry.getKey(), entry.getValue()); + properties.forEach(new BiConsumer<>() { + @Override + public void accept(String key, String value) { + EnvName envName = new EnvName(key); + EnvEntry envEntry = env.get(envName); + if (envEntry == null) { + env.put(envName, new EnvEntry(key, value)); + } else { + envEntry.add(key, value); + } + EnvVars.this.names.add(key); + EnvVars.this.names.add(toLowerCaseAndDotted(key)); } - this.names.add(entry.getKey()); - this.names.add(toLowerCaseAndDotted(entry.getKey())); - } + }); } public String get(final String propertyName) {