diff --git a/core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java b/core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java index 647fbfb0cc..afd7202892 100644 --- a/core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java +++ b/core-java-8/src/test/java/com/baeldung/counter/CounterUtil.java @@ -38,23 +38,14 @@ public class CounterUtil { public static void counterWithMutableInteger(Map counterMap) { for (String country : COUNTRY_NAMES) { - MutableInteger oldValue = counterMap.get(country); - if (oldValue != null) { - oldValue.increment(); - } else { - counterMap.put(country, new MutableInteger(1)); - } + counterMap.compute(country, (k, v) -> v == null ? new MutableInteger(0) : v) + .increment(); } } public static void counterWithPrimitiveArray(Map counterMap) { for (String country : COUNTRY_NAMES) { - int[] oldCounter = counterMap.get(country); - if (oldCounter != null) { - oldCounter[0] += 1; - } else { - counterMap.put(country, new int[] { 1 }); - } + counterMap.compute(country, (k, v) -> v == null ? new int[] { 0 } : v)[0]++; } }