Skip to content

Commit 9627971

Browse files
eralmas7pivovarit
authored andcommitted
BAEL-1375 Using compute for every usage. (eugenp#3242)
1 parent 04df1cf commit 9627971

1 file changed

Lines changed: 3 additions & 12 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,14 @@ public int getCount() {
3838

3939
public static void counterWithMutableInteger(Map<String, MutableInteger> counterMap) {
4040
for (String country : COUNTRY_NAMES) {
41-
MutableInteger oldValue = counterMap.get(country);
42-
if (oldValue != null) {
43-
oldValue.increment();
44-
} else {
45-
counterMap.put(country, new MutableInteger(1));
46-
}
41+
counterMap.compute(country, (k, v) -> v == null ? new MutableInteger(0) : v)
42+
.increment();
4743
}
4844
}
4945

5046
public static void counterWithPrimitiveArray(Map<String, int[]> counterMap) {
5147
for (String country : COUNTRY_NAMES) {
52-
int[] oldCounter = counterMap.get(country);
53-
if (oldCounter != null) {
54-
oldCounter[0] += 1;
55-
} else {
56-
counterMap.put(country, new int[] { 1 });
57-
}
48+
counterMap.compute(country, (k, v) -> v == null ? new int[] { 0 } : v)[0]++;
5849
}
5950
}
6051

0 commit comments

Comments
 (0)