From ae109dfb7cf220cc1deffb010a226bf55acc81f7 Mon Sep 17 00:00:00 2001 From: Jose Bob Santos Jr Date: Fri, 15 Dec 2017 09:50:51 +0800 Subject: [PATCH] BAEL-1330 Primitive Type Streams in Java 8 (#3106) * initial commit for first article evaluation * fixed compilation error * removed final keywords for compliance * added unit tests and refactored as needed * refactored application-context.xml to use self-closing tags * renamed tests to use UnitTest suffix * removed unnecessary annotation * added code samples for primitive streams * removed sample code and tests for evaluation article * renamed unit test to proper unit test naming * added a test to show boxed method * added the assertion to the test fox boxed * changed mapToInt test to use Arrays.asList instead of a POJO as per Grzegorz and Eugen --- .../com/baeldung/stream/PrimitiveStreams.java | 23 +++++ .../stream/PrimitiveStreamsUnitTest.java | 97 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/stream/PrimitiveStreams.java create mode 100644 core-java-8/src/test/java/com/baeldung/stream/PrimitiveStreamsUnitTest.java diff --git a/core-java-8/src/main/java/com/baeldung/stream/PrimitiveStreams.java b/core-java-8/src/main/java/com/baeldung/stream/PrimitiveStreams.java new file mode 100644 index 0000000000..b0afb65a35 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/stream/PrimitiveStreams.java @@ -0,0 +1,23 @@ +package com.baeldung.stream; + +import java.util.Arrays; +import java.util.stream.IntStream; + +class PrimitiveStreams { + + int min(int[] integers) { + return Arrays.stream(integers).min().getAsInt(); + } + + int max(int... integers) { + return IntStream.of(integers).max().getAsInt(); + } + + int sum(int... integers) { + return IntStream.of(integers).sum(); + } + + double avg(int... integers) { + return IntStream.of(integers).average().getAsDouble(); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/stream/PrimitiveStreamsUnitTest.java b/core-java-8/src/test/java/com/baeldung/stream/PrimitiveStreamsUnitTest.java new file mode 100644 index 0000000000..75078d75bc --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/stream/PrimitiveStreamsUnitTest.java @@ -0,0 +1,97 @@ +package com.baeldung.stream; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PrimitiveStreamsUnitTest { + + private PrimitiveStreams streams = new PrimitiveStreams(); + + @Test + public void givenAnArrayOfIntegersWhenMinIsCalledThenCorrectMinIsReturned() { + int[] integers = new int[] {20, 98, 12, 7, 35}; + int min = streams.min(integers); // returns 7 + + assertEquals(7, min); + } + + @Test + public void givenAnArrayOfIntegersWhenMaxIsCalledThenCorrectMaxIsReturned() { + int max = streams.max(20, 98, 12, 7, 35); + + assertEquals(98, max); + } + + @Test + public void givenAnArrayOfIntegersWhenSumIsCalledThenCorrectSumIsReturned() { + int sum = streams.sum(20, 98, 12, 7, 35); + + assertEquals(172, sum); + } + + @Test + public void givenAnArrayOfIntegersWhenAvgIsCalledThenCorrectAvgIsReturned() { + double avg = streams.avg(20, 98, 12, 7, 35); + + assertTrue(34.4 == avg); + } + + @Test + public void givenARangeOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() { + int sum = IntStream.range(1, 10).sum(); + + assertEquals(45, sum); + } + + @Test + public void givenARangeClosedOfIntegersWhenIntStreamSumIsCalledThenCorrectSumIsReturned() { + int sum = IntStream.rangeClosed(1, 10).sum(); + + assertEquals(55, sum); + } + + @Test + public void givenARangeWhenForEachIsCalledThenTheIndicesWillBePrinted() { + IntStream.rangeClosed(1, 5).parallel().forEach(System.out::println); + } + + @Test + public void givenAnArrayWhenSumIsCalledThenTheCorrectSumIsReturned() { + + int sum = Arrays.asList(33,45).stream().mapToInt(a -> a).sum(); + + assertEquals(78, sum); + } + + @Test + public void givenAnIntStreamThenGetTheEvenIntegers() { + List evenInts = IntStream.rangeClosed(1, 10) + .filter(i -> i % 2 == 0) + .boxed() + .collect(Collectors.toList()); + + List expected = IntStream.of(2, 4, 6, 8, 10).boxed().collect(Collectors.toList()); + + assertEquals(expected, evenInts); + } + + class Person { + private int age; + + Person(int age) { + this.age = age; + } + + int getAge() { + return age; + } + } +} \ No newline at end of file