From 99bb67a4132445b831921fe9c71fa682a10939ac Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Sun, 21 Mar 2021 23:22:54 +0000 Subject: [PATCH] BAEL-4583-Generic-arrays-streams --- .../core-java-arrays-guides/pom.xml | 6 ++ .../genericarrays/ListToArrayUnitTest.java | 1 - .../genericarrays/StreamToArrayUnitTest.java | 77 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/StreamToArrayUnitTest.java diff --git a/core-java-modules/core-java-arrays-guides/pom.xml b/core-java-modules/core-java-arrays-guides/pom.xml index df8639820d..263954445e 100644 --- a/core-java-modules/core-java-arrays-guides/pom.xml +++ b/core-java-modules/core-java-arrays-guides/pom.xml @@ -24,6 +24,12 @@ jmh-generator-annprocess ${jmh.version} + + org.assertj + assertj-core + 3.19.0 + test + diff --git a/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/ListToArrayUnitTest.java b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/ListToArrayUnitTest.java index 5fd0385181..df7e0566ed 100644 --- a/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/ListToArrayUnitTest.java +++ b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/ListToArrayUnitTest.java @@ -2,7 +2,6 @@ package com.baeldung.genericarrays; import org.junit.Test; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; diff --git a/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/StreamToArrayUnitTest.java b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/StreamToArrayUnitTest.java new file mode 100644 index 0000000000..5dea901b25 --- /dev/null +++ b/core-java-modules/core-java-arrays-guides/src/test/java/com/baeldung/genericarrays/StreamToArrayUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.genericarrays; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.IntFunction; +import java.util.stream.Stream; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StreamToArrayUnitTest { + + @Test + public void givenAStream_thenCanGetArrayOfObject() { + Object[] strings = Stream.of("A", "AAA", "B", "AAB", "C") + .filter(string -> string.startsWith("A")) + .toArray(); + + assertThat(strings).containsExactly("A", "AAA", "AAB"); + assertThat(strings).isNotInstanceOf(String[].class); + } + + @Test + public void givenAStream_thenCanGetArrayOfString() { + String[] strings = Stream.of("A", "AAA", "B", "AAB", "C") + .filter(string -> string.startsWith("A")) + .toArray(String[]::new); + + assertThat(strings).containsExactly("A", "AAA", "AAB"); + assertThat(strings).isInstanceOf(String[].class); + } + + @SuppressWarnings("unchecked") + @Test + public void givenAStream_whenConvertToOptional_thenCanGetArrayOfOptional() { + Stream> stream = Stream.of("A", "AAA", "B", "AAB", "C") + .filter(string -> string.startsWith("A")) + .map(Optional::of); + Optional[] strings = stream + .toArray(Optional[]::new); + + assertThat(strings).containsExactly(Optional.of("A"), + Optional.of("AAA"), + Optional.of("AAB")); + assertThat(strings).isInstanceOf(Optional[].class); + } + + @Test + public void givenAStream_whenConvertToOptional_thenCanGetArrayOfOptionalWithHelper() { + Optional[] strings = Stream.of("A", "AAA", "B", "AAB", "C") + .filter(string -> string.startsWith("A")) + .map(Optional::of) + .toArray(genericArray(Optional[]::new)); + + assertThat(strings).containsExactly(Optional.of("A"), + Optional.of("AAA"), + Optional.of("AAB")); + assertThat(strings).isInstanceOf(Optional[].class); + } + + @Test + public void whenInvalidUseOfGenericArray_thenIllegalCast() { + assertThatThrownBy(() -> { + ArrayList[] lists = Stream.of(singletonList("A")) + .toArray(genericArray(List[]::new)); + }).isInstanceOf(ClassCastException.class); + } + + @SuppressWarnings("unchecked") + private static IntFunction genericArray(IntFunction arrayCreator) { + return size -> (R[])arrayCreator.apply(size); + } +}