Skip to content

Commit 213a6e2

Browse files
Tian Baoqiangpivovarit
authored andcommitted
#BAEL-397: use toMap instead of creating an empty hashmap (eugenp#2167)
* BAEL-397: use toMap instead of creating an empty hashmap * BAEL-397: static import toMap for cleaner code
1 parent 3ae9699 commit 213a6e2

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

‎metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import org.junit.Ignore;
66
import org.junit.Test;
77

8-
import java.util.HashMap;
98
import java.util.Map;
109

1110
import static java.util.concurrent.TimeUnit.SECONDS;
11+
import static java.util.stream.Collectors.toMap;
1212
import static org.hamcrest.Matchers.*;
1313
import static org.junit.Assert.assertEquals;
1414
import static org.junit.Assert.assertThat;
@@ -123,15 +123,14 @@ public void givenBucketTimer_whenRecord_thenStatsCalculated() throws Exception {
123123
.getTotalTime()
124124
.intValue());
125125

126-
final Map<String, Long> metricMap = new HashMap<>(3);
127-
timer
126+
final Map<String, Long> metricMap = timer
128127
.getMonitors()
129128
.stream()
130129
.filter(monitor -> monitor
131130
.getConfig()
132131
.getTags()
133132
.containsKey("servo.bucket"))
134-
.forEach(monitor -> metricMap.put(getMonitorTagValue(monitor, "servo.bucket"), (Long) monitor.getValue()));
133+
.collect(toMap(monior -> getMonitorTagValue(monior, "servo.bucket"), monitor -> (Long) monitor.getValue()));
135134

136135
assertThat(metricMap, allOf(hasEntry("bucket=2s", 0L), hasEntry("bucket=5s", 1L), hasEntry("bucket=overflow", 1L)));
137136
}
@@ -177,10 +176,10 @@ public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Excepti
177176
.getValue()
178177
.intValue());
179178

180-
final Map<String, Number> metricMap = new HashMap<>(10);
181-
timer
179+
final Map<String, Number> metricMap = timer
182180
.getMonitors()
183-
.forEach(monitor -> metricMap.put(getMonitorTagValue(monitor, "statistic"), (Number) monitor.getValue()));
181+
.stream()
182+
.collect(toMap(monitor -> getMonitorTagValue(monitor, "statistic"), monitor -> (Number) monitor.getValue()));
184183

185184
assertThat(metricMap.keySet(), containsInAnyOrder("count", "totalTime", "max", "min", "variance", "stdDev", "avg", "percentile_99", "percentile_95", "percentile_90"));
186185
}

0 commit comments

Comments
 (0)