From 130ba52379326a06e05e7d3613afb8ba6992e6e8 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Fri, 3 Jan 2020 07:49:52 +0100 Subject: [PATCH 1/2] BAEL-3688: Add examples of a constructor and a setter injection types (#8452) --- .../com/baeldung/autowire/sample/FooService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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(); } From c4497b7d3b588e6a74b61da7aca90a2356ba6896 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Fri, 3 Jan 2020 07:51:16 +0100 Subject: [PATCH 2/2] BAEL-3608: Add Map.merge() usage example (#8455) --- .../duplicatescounter/DuplicatesCounter.java | 18 ++++++++++++++---- .../DuplicatesCounterUnitTest.java | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) 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); }