From 1d0afe4f07c1e823166102379b0ff4cf3c2f0227 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Wed, 16 Aug 2023 02:40:47 +0200 Subject: [PATCH] Convert an ArrayList of String to a String Array in Java (#14583) * Convert an ArrayList of String to a String Array in Java * fixing typo * module moving --- .../core-java-arrays-convert/pom.xml | 1 - .../pom.xml | 3 +- .../ArrayListToArrayUnitTest.java | 46 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayListToArrayUnitTest.java diff --git a/core-java-modules/core-java-arrays-convert/pom.xml b/core-java-modules/core-java-arrays-convert/pom.xml index c90b60e09a..480e1b3f07 100644 --- a/core-java-modules/core-java-arrays-convert/pom.xml +++ b/core-java-modules/core-java-arrays-convert/pom.xml @@ -20,5 +20,4 @@ ${commons-lang3.version} - \ No newline at end of file diff --git a/core-java-modules/core-java-collections-conversions-2/pom.xml b/core-java-modules/core-java-collections-conversions-2/pom.xml index 0a30cbeae6..7b7533fc39 100644 --- a/core-java-modules/core-java-collections-conversions-2/pom.xml +++ b/core-java-modules/core-java-collections-conversions-2/pom.xml @@ -44,7 +44,6 @@ 0.10.3 11 - 3.1.1 + 3.1.1 - \ No newline at end of file diff --git a/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayListToArrayUnitTest.java b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayListToArrayUnitTest.java new file mode 100644 index 0000000000..63cdf22f3a --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayListToArrayUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.arrayconversion; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.Lists; + +public class ArrayListToArrayUnitTest { + private static final List INPUT_LIST = Lists.newArrayList("Michael Bolton", "Michael Jackson", "Guns and Roses", "Bryan Adams", "Air Supply"); + private static final String[] EXPECTED_ARRAY = new String[] { "Michael Bolton", "Michael Jackson", "Guns and Roses", "Bryan Adams", "Air Supply" }; + + @Test + void whenUsingForLoop_thenGetExpectedResult() { + String[] result = new String[INPUT_LIST.size()]; + for (int i = 0; i < INPUT_LIST.size(); i++) { + result[i] = INPUT_LIST.get(i); + } + assertArrayEquals(EXPECTED_ARRAY, result); + } + + @Test + void whenUsingListToArray_thenGetExpectedResult() { + String[] result = new String[INPUT_LIST.size()]; + INPUT_LIST.toArray(result); + assertArrayEquals(EXPECTED_ARRAY, result); + + String[] result2 = INPUT_LIST.toArray(new String[0]); + assertArrayEquals(EXPECTED_ARRAY, result2); + } + + @Test + void whenUsingStreamApi_thenGetExpectedResult() { + String[] result = INPUT_LIST.stream() + .toArray(String[]::new); + assertArrayEquals(EXPECTED_ARRAY, result); + } + + @Test + void whenUsingListToArrayInJava11_thenGetExpectedResult() { + String[] result = INPUT_LIST.toArray(String[]::new); + assertArrayEquals(EXPECTED_ARRAY, result); + } +} \ No newline at end of file