From 7f1acd21bfa9932ca7a09d35d618b294ba112fdb Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Tue, 21 Jul 2020 21:30:17 -0500 Subject: [PATCH 1/6] BAEL-4387 Add files to handle Array to ArrayList convertion --- .../ArrayToListConversion.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java b/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java new file mode 100644 index 0000000000..7e1002b22c --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java @@ -0,0 +1,33 @@ +package com.baeldung.arrayconvertion; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayToListConversion { + + public static void main(String[] args) { + System.out.println("Array.asList()"); + arrayAsList(); + System.out.println("\nArrayList<>(Arrays.asList())"); + independentArray(); + } + + private static void arrayAsList() { + String[] stringArray = new String[] { "A", "B", "C", "D" }; + List stringList = Arrays.asList(stringArray); + System.out.println(stringList); // [A, B, C, D] + stringList.set(0, "E"); + System.out.println(stringList); // [E, B, C, D] + System.out.println(Arrays.toString(stringArray)); // [E, B, C, D] + } + + private static void independentArray() { + String[] stringArray = new String[] { "A", "B", "C", "D" }; + List stringList = new ArrayList<>(Arrays.asList(stringArray)); + System.out.println(stringList); // [A, B, C, D] + stringList.set(0, "E"); + System.out.println(stringList); // [E, B, C, D] + System.out.println(Arrays.toString(stringArray)); // [A, B, C, D] + } +} \ No newline at end of file From 2679498f32546dfd50d0a04896a83a3ed07548ba Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Fri, 7 Aug 2020 22:13:33 -0500 Subject: [PATCH 2/6] BAEL-4387 Add the code inside a test --- .../ArrayToListConversion.java | 33 ------------------- .../ArrayToListConvertion.java | 32 ++++++++++++++++++ 2 files changed, 32 insertions(+), 33 deletions(-) delete mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java create mode 100644 java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java b/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java deleted file mode 100644 index 7e1002b22c..0000000000 --- a/java-collections-conversions-2/src/main/java/com/baeldung/arrayconvertion/ArrayToListConversion.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.arrayconvertion; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ArrayToListConversion { - - public static void main(String[] args) { - System.out.println("Array.asList()"); - arrayAsList(); - System.out.println("\nArrayList<>(Arrays.asList())"); - independentArray(); - } - - private static void arrayAsList() { - String[] stringArray = new String[] { "A", "B", "C", "D" }; - List stringList = Arrays.asList(stringArray); - System.out.println(stringList); // [A, B, C, D] - stringList.set(0, "E"); - System.out.println(stringList); // [E, B, C, D] - System.out.println(Arrays.toString(stringArray)); // [E, B, C, D] - } - - private static void independentArray() { - String[] stringArray = new String[] { "A", "B", "C", "D" }; - List stringList = new ArrayList<>(Arrays.asList(stringArray)); - System.out.println(stringList); // [A, B, C, D] - stringList.set(0, "E"); - System.out.println(stringList); // [E, B, C, D] - System.out.println(Arrays.toString(stringArray)); // [A, B, C, D] - } -} \ No newline at end of file diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java new file mode 100644 index 0000000000..f0f3fda42e --- /dev/null +++ b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java @@ -0,0 +1,32 @@ +package com.baeldung.arrayconvertion; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayToListConvertion { + + @Test(expected = UnsupportedOperationException.class) + public void givenAnArray_whenConvertToList_returnUnmodifiableList() { + String[] stringArray = new String[] { "A", "B", "C", "D" }; + List stringList = Arrays.asList(stringArray); + System.out.println(stringList); + stringList.set(0, "E"); + System.out.println(stringList); + System.out.println(Arrays.toString(stringArray)); + stringList.add("F"); + } + + @Test + public void givenAnArray_whenConvertToList_returnModifiableList() { + String[] stringArray = new String[] { "A", "B", "C", "D" }; + List stringList = new ArrayList<>(Arrays.asList(stringArray)); + System.out.println(stringList); + stringList.set(0, "E"); + System.out.println(stringList); + System.out.println(Arrays.toString(stringArray)); + stringList.add("F"); + } +} From 3bfaf7a5563f9faf803672ab01e38f5a23298aca Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Wed, 19 Aug 2020 06:30:51 -0500 Subject: [PATCH 3/6] BAEL-4387 Fix method name --- .../ArrayToListConversion.java} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename java-collections-conversions-2/src/test/java/com/baeldung/{arrayconvertion/ArrayToListConvertion.java => arrayconversion/ArrayToListConversion.java} (79%) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java similarity index 79% rename from java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java rename to java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java index f0f3fda42e..33927286c2 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconvertion/ArrayToListConvertion.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java @@ -1,4 +1,4 @@ -package com.baeldung.arrayconvertion; +package com.baeldung.arrayconversion; import org.junit.Test; @@ -6,10 +6,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class ArrayToListConvertion { +public class ArrayToListConversion { @Test(expected = UnsupportedOperationException.class) - public void givenAnArray_whenConvertToList_returnUnmodifiableList() { + public void givenAnArray_whenConvertingToList_returnUnmodifiableList() { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = Arrays.asList(stringArray); System.out.println(stringList); @@ -20,7 +20,7 @@ public class ArrayToListConvertion { } @Test - public void givenAnArray_whenConvertToList_returnModifiableList() { + public void givenAnArray_whenConvertingToList_returnModifiableList() { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = new ArrayList<>(Arrays.asList(stringArray)); System.out.println(stringList); From bd0a7a2822130409a619c391d25d6051d93b33a3 Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Wed, 19 Aug 2020 06:34:26 -0500 Subject: [PATCH 4/6] BAEL-4387 Improve method naming --- .../com/baeldung/arrayconversion/ArrayToListConversion.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java index 33927286c2..09641cd1d5 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java @@ -9,7 +9,7 @@ import java.util.List; public class ArrayToListConversion { @Test(expected = UnsupportedOperationException.class) - public void givenAnArray_whenConvertingToList_returnUnmodifiableList() { + public void givenAnArray_whenConvertingToList_returnUnmodifiableListUnitTest() { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = Arrays.asList(stringArray); System.out.println(stringList); @@ -20,7 +20,7 @@ public class ArrayToListConversion { } @Test - public void givenAnArray_whenConvertingToList_returnModifiableList() { + public void givenAnArray_whenConvertingToList_returnModifiableListUnitTest() { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = new ArrayList<>(Arrays.asList(stringArray)); System.out.println(stringList); From cc308b34edeb7567b2d4f444041e5f64ba4db131 Mon Sep 17 00:00:00 2001 From: bfontana Date: Wed, 19 Aug 2020 21:10:47 -0300 Subject: [PATCH 5/6] Update ArrayToListConversion.java --- .../com/baeldung/arrayconversion/ArrayToListConversion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java index 09641cd1d5..551661810d 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java @@ -6,7 +6,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class ArrayToListConversion { +public class ArrayToListConversionUnitTest { @Test(expected = UnsupportedOperationException.class) public void givenAnArray_whenConvertingToList_returnUnmodifiableListUnitTest() { From 68154fa54070f1a038d116f479f679b4e1eb31cb Mon Sep 17 00:00:00 2001 From: Bruno Fontana Date: Wed, 19 Aug 2020 21:50:54 -0300 Subject: [PATCH 6/6] Fixing class file name --- ...ayToListConversion.java => ArrayToListConversionUnitTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/{ArrayToListConversion.java => ArrayToListConversionUnitTest.java} (100%) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java similarity index 100% rename from java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversion.java rename to java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java