From c1fce0bcd25144a1fb7e429aa7c6127cea49daa8 Mon Sep 17 00:00:00 2001 From: RoscoeLotriet Date: Mon, 24 Sep 2018 10:26:15 +0200 Subject: [PATCH 1/3] Implemented Stream if-else logic class --- .../conditional/StreamForEachIfElseLogic.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java diff --git a/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java b/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java new file mode 100644 index 0000000000..e8800415dc --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java @@ -0,0 +1,30 @@ +package com.baeldung.stream.conditional; + +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StreamForEachIfElseLogic { + + private static final Logger LOG = LoggerFactory.getLogger(StreamForEachIfElseLogic.class); + + public static void main(String[] args) { + + ifElseLogic(); + + } + + private static void ifElseLogic() { + + Stream integers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + integers.forEach(i -> { + if (i.intValue() % 2 == 0) { + LOG.info("{} is even", i); + } else { + LOG.info("{} is odd", i); + } + }); + } +} From 9b6a043cbb0378d75c385dfabf89dfc3fa675781 Mon Sep 17 00:00:00 2001 From: RoscoeLotriet Date: Fri, 28 Sep 2018 06:02:34 +0200 Subject: [PATCH 2/3] Updated if-else logic and added filter method examples --- .../conditional/StreamForEachIfElseLogic.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java b/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java index e8800415dc..b8ef521b41 100644 --- a/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java +++ b/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java @@ -1,5 +1,7 @@ package com.baeldung.stream.conditional; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import org.slf4j.Logger; @@ -17,14 +19,24 @@ public class StreamForEachIfElseLogic { private static void ifElseLogic() { - Stream integers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + ints.stream() + .forEach(i -> { + if (i.intValue() % 2 == 0) { + LOG.info("{} is even", i); + } else { + LOG.info("{} is odd", i); + } + }); + + Stream evenIntegers = ints.stream() + .filter(i -> i.intValue() % 2 == 0); + Stream oddIntegers = ints.stream() + .filter(i -> i.intValue() % 2 != 0); + + evenIntegers.forEach(i -> LOG.info("{} is even", i)); + oddIntegers.forEach(i -> LOG.info("{} is odd", i)); - integers.forEach(i -> { - if (i.intValue() % 2 == 0) { - LOG.info("{} is even", i); - } else { - LOG.info("{} is odd", i); - } - }); } } From 379eab82d6d2e26280b006808fa41b08f666d2e9 Mon Sep 17 00:00:00 2001 From: RoscoeLotriet Date: Mon, 1 Oct 2018 14:18:24 +0200 Subject: [PATCH 3/3] Implemented Stream forEach if/else unit test --- .../conditional/StreamForEachIfElseLogic.java | 42 ------------------- .../StreamForEachIfElseUnitTest.java | 41 ++++++++++++++++++ 2 files changed, 41 insertions(+), 42 deletions(-) delete mode 100644 core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java create mode 100644 core-java-8/src/test/java/com/baeldung/stream/conditional/StreamForEachIfElseUnitTest.java diff --git a/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java b/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java deleted file mode 100644 index b8ef521b41..0000000000 --- a/core-java-8/src/main/java/com/baeldung/stream/conditional/StreamForEachIfElseLogic.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.stream.conditional; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Stream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class StreamForEachIfElseLogic { - - private static final Logger LOG = LoggerFactory.getLogger(StreamForEachIfElseLogic.class); - - public static void main(String[] args) { - - ifElseLogic(); - - } - - private static void ifElseLogic() { - - List ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - - ints.stream() - .forEach(i -> { - if (i.intValue() % 2 == 0) { - LOG.info("{} is even", i); - } else { - LOG.info("{} is odd", i); - } - }); - - Stream evenIntegers = ints.stream() - .filter(i -> i.intValue() % 2 == 0); - Stream oddIntegers = ints.stream() - .filter(i -> i.intValue() % 2 != 0); - - evenIntegers.forEach(i -> LOG.info("{} is even", i)); - oddIntegers.forEach(i -> LOG.info("{} is odd", i)); - - } -} diff --git a/core-java-8/src/test/java/com/baeldung/stream/conditional/StreamForEachIfElseUnitTest.java b/core-java-8/src/test/java/com/baeldung/stream/conditional/StreamForEachIfElseUnitTest.java new file mode 100644 index 0000000000..b5d26eb6a8 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/stream/conditional/StreamForEachIfElseUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.stream.conditional; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Test; + +public class StreamForEachIfElseUnitTest { + + @Test + public final void givenIntegerStream_whenCheckingIntegerParityWithIfElse_thenEnsureCorrectParity() { + List ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + ints.stream() + .forEach(i -> { + if (i.intValue() % 2 == 0) { + Assert.assertTrue(i.intValue() + " is not even", i.intValue() % 2 == 0); + } else { + Assert.assertTrue(i.intValue() + " is not odd", i.intValue() % 2 != 0); + } + }); + + } + + @Test + public final void givenIntegerStream_whenCheckingIntegerParityWithStreamFilter_thenEnsureCorrectParity() { + List ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + Stream evenIntegers = ints.stream() + .filter(i -> i.intValue() % 2 == 0); + Stream oddIntegers = ints.stream() + .filter(i -> i.intValue() % 2 != 0); + + evenIntegers.forEach(i -> Assert.assertTrue(i.intValue() + " is not even", i.intValue() % 2 == 0)); + oddIntegers.forEach(i -> Assert.assertTrue(i.intValue() + " is not odd", i.intValue() % 2 != 0)); + + } + +}