diff --git a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/duplicatescounter/DuplicatesCounter.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/duplicatescounter/DuplicatesCounter.java index b5138eed7d..e0c0b723e0 100644 --- a/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/duplicatescounter/DuplicatesCounter.java +++ b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/duplicatescounter/DuplicatesCounter.java @@ -24,11 +24,21 @@ public class DuplicatesCounter { return resultMap; } - public static Map countByClassicalLoopWithMapCompute(List inputList) { + public static Map countByForEachLoopWithGetOrDefault(List inputList) { Map resultMap = new HashMap<>(); - for (T element : inputList) { - resultMap.compute(element, (k, v) -> v == null ? 1 : v + 1); - } + inputList.forEach(e -> resultMap.put(e, resultMap.getOrDefault(e, 0L) + 1L)); + return resultMap; + } + + public static Map countByForEachLoopWithMapCompute(List inputList) { + Map resultMap = new HashMap<>(); + inputList.forEach(e -> resultMap.compute(e, (k, v) -> v == null ? 1L : v + 1L)); + return resultMap; + } + + public static Map countByForEachLoopWithMapMerge(List inputList) { + Map resultMap = new HashMap<>(); + inputList.forEach(e -> resultMap.merge(e, 1L, Long::sum)); return resultMap; } diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/duplicatescounter/DuplicatesCounterUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/duplicatescounter/DuplicatesCounterUnitTest.java index be9ccded9b..8d6825c590 100644 --- a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/duplicatescounter/DuplicatesCounterUnitTest.java +++ b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/duplicatescounter/DuplicatesCounterUnitTest.java @@ -11,7 +11,6 @@ import static org.assertj.core.data.MapEntry.entry; class DuplicatesCounterUnitTest { - private static List INPUT_LIST = Lists.list( "expect1", "expect2", "expect2", @@ -24,10 +23,21 @@ class DuplicatesCounterUnitTest { verifyResult(result); } + @Test + void givenInput_whenCountByForEachLoopWithGetOrDefault_thenGetResultMap() { + Map result = DuplicatesCounter.countByForEachLoopWithGetOrDefault(INPUT_LIST); + verifyResult(result); + } @Test - void givenInput_whenCountByClassicalLoopWithMapCompute_thenGetResultMap() { - Map result = DuplicatesCounter.countByClassicalLoopWithMapCompute(INPUT_LIST); + void givenInput_whenCountByForEachLoopWithMapCompute_thenGetResultMap() { + Map result = DuplicatesCounter.countByForEachLoopWithMapCompute(INPUT_LIST); + verifyResult(result); + } + + @Test + void givenInput_whenCountByForEachLoopWithMapMerge_thenGetResultMap() { + Map result = DuplicatesCounter.countByForEachLoopWithMapMerge(INPUT_LIST); verifyResult(result); } diff --git a/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java b/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java index c55d93da11..8a073279bc 100644 --- a/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java +++ b/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java @@ -3,6 +3,10 @@ package com.baeldung.autowire.sample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +/** + * This class presents a field, a constructor, and a setter injection type. + * Usually, we'd stick with a single approach for a given property. This is just an educational code. + */ @Component public class FooService { @@ -10,6 +14,16 @@ public class FooService { @FormatterType("Foo") private Formatter formatter; + @Autowired + public FooService(@FormatterType("Foo") Formatter formatter) { + this.formatter = formatter; + } + + @Autowired + public void setFormatter(@FormatterType("Foo") Formatter formatter) { + this.formatter = formatter; + } + public String doStuff() { return formatter.format(); }