From b2f3115cfda2dd24d38b29f9553cb462f7c727bb Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Sun, 21 Oct 2018 00:26:30 +0530 Subject: [PATCH 1/2] BAEL-9040 Two quick improvements to the foreach article -Added more tests for forEach tutorial --- .../baeldung/java8/Java8ForEachUnitTest.java | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java index 6a485e939f..7840c84b7d 100644 --- a/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java @@ -4,8 +4,15 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; import java.util.function.Consumer; public class Java8ForEachUnitTest { @@ -29,8 +36,18 @@ public class Java8ForEachUnitTest { } // Java 8 - forEach - LOG.debug("--- forEach method ---"); - names.forEach(name -> LOG.debug(name)); + names.forEach(name -> { + System.out.println(name); + }); + + LOG.debug("--- Print Consumer ---"); + Consumer printConsumer = new Consumer() { + public void accept(String name) { + System.out.println(name); + }; + }; + + names.forEach(printConsumer); // Anonymous inner class that implements Consumer interface LOG.debug("--- Anonymous inner class ---"); @@ -40,17 +57,45 @@ public class Java8ForEachUnitTest { } }); - // Create a Consumer implementation to then use in a forEach method - Consumer consumerNames = name -> { - LOG.debug(name); - }; - LOG.debug("--- Implementation of Consumer interface ---"); - names.forEach(consumerNames); + // Java 8 - forEach - Lambda Syntax + LOG.debug("--- forEach method ---"); + names.forEach(name -> LOG.debug(name)); - // Print elements using a Method Reference + // Java 8 - forEach - Print elements using a Method Reference LOG.debug("--- Method Reference ---"); names.forEach(LOG::debug); + } + @Test + public void givenList_thenIterateAndPrintResults() { + List names = Arrays.asList("Larry", "Steve", "James"); + + names.forEach(System.out::println); + } + + @Test + public void givenSet_thenIterateAndPrintResults() { + Set uniqueNames = new HashSet<>(Arrays.asList("Larry", "Steve", "James")); + + uniqueNames.forEach(System.out::println); + } + + @Test + public void givenQueue_thenIterateAndPrintResults() { + Queue namesQueue = new ArrayDeque<>(Arrays.asList("Larry", "Steve", "James")); + + namesQueue.forEach(System.out::println); + } + + @Test + public void givenMap_thenIterateAndPrintResults() { + Map namesMap = new HashMap<>(); + namesMap.put(1, "Larry"); + namesMap.put(2, "Steve"); + namesMap.put(3, "James"); + + namesMap.entrySet() + .forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue())); } } From 6ef143f57060e7dd32302a108b3439e0368c59f0 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 23 Oct 2018 22:00:06 +0530 Subject: [PATCH 2/2] BAEL-9040 Two quick improvements to the foreach article -Added Map's forEach BiConsumer example --- .../baeldung/java8/Java8ForEachUnitTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java b/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java index 7840c84b7d..f5201f54cf 100644 --- a/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java @@ -1,9 +1,5 @@ package com.baeldung.java8; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -15,6 +11,10 @@ import java.util.Queue; import java.util.Set; import java.util.function.Consumer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class Java8ForEachUnitTest { private static final Logger LOG = LoggerFactory.getLogger(Java8ForEachUnitTest.class); @@ -98,4 +98,14 @@ public class Java8ForEachUnitTest { .forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue())); } + @Test + public void givenMap_whenUsingBiConsumer_thenIterateAndPrintResults2() { + Map namesMap = new HashMap<>(); + namesMap.put(1, "Larry"); + namesMap.put(2, "Steve"); + namesMap.put(3, "James"); + + namesMap.forEach((key, value) -> System.out.println(key + " " + value)); + } + }