diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemo.java new file mode 100644 index 0000000000..d3e88029c9 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemo.java @@ -0,0 +1,14 @@ +package com.baeldung.indexoutofbounds; + +import java.util.ArrayList; +import java.util.List; + +public class CopyListUsingAddAllMethodDemo { + static List copyList(List source) { + List destination = new ArrayList<>(); + + destination.addAll(source); + + return destination; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemo.java new file mode 100644 index 0000000000..6f897fdfbd --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemo.java @@ -0,0 +1,10 @@ +package com.baeldung.indexoutofbounds; + +import java.util.Collections; +import java.util.List; + +public class CopyListUsingCollectionsCopyMethodDemo { + static void copyList(List source, List destination) { + Collections.copy(destination, source); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemo.java new file mode 100644 index 0000000000..6c6da80a46 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemo.java @@ -0,0 +1,10 @@ +package com.baeldung.indexoutofbounds; + +import java.util.ArrayList; +import java.util.List; + +public class CopyListUsingConstructorDemo { + static List copyList(List source) { + return new ArrayList<>(source); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemo.java new file mode 100644 index 0000000000..1b5a4c2e01 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemo.java @@ -0,0 +1,12 @@ +package com.baeldung.indexoutofbounds; + +import java.util.List; +import java.util.stream.Collectors; + +public class CopyListUsingJava8StreamDemo { + static List copyList(List source) { + return source + .stream() + .collect(Collectors.toList()); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemo.java new file mode 100644 index 0000000000..3088811d60 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemo.java @@ -0,0 +1,13 @@ +package com.baeldung.indexoutofbounds; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class IndexOutOfBoundsExceptionDemo { + static List copyList(List source) { + List destination = new ArrayList<>(source.size()); + Collections.copy(destination, source); + return destination; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemoUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemoUnitTest.java new file mode 100644 index 0000000000..c16c7bca7c --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingAddAllMethodDemoUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.indexoutofbounds; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CopyListUsingAddAllMethodDemoUnitTest { + + @Test + void whenPassValidArrayList_thenCopyListUsingAddAllMethod() { + List source = Arrays.asList(11, 22, 33); + + assertEquals(source, CopyListUsingAddAllMethodDemo.copyList(source)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemoUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemoUnitTest.java new file mode 100644 index 0000000000..49d66b855c --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingCollectionsCopyMethodDemoUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.indexoutofbounds; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class CopyListUsingCollectionsCopyMethodDemoUnitTest { + @Test + void whenCopyListUsingCollectionsCopy_thenOverrideAllDestinationListValues() { + List source = Arrays.asList(11, 22, 33); + List destination = Arrays.asList(1, 2, 3); + + CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination); + + assertEquals(source, destination); + } + + @Test + void whenCopyListUsingCollectionsCopy_thenOverrideInitialDestinationValuesAndOthersShouldBeUnchanged(){ + List source = Arrays.asList(11, 22, 33); + List destination = Arrays.asList(1, 2, 3, 4, 5); + List expectedList = Arrays.asList(11, 22, 33, 4, 5); + + CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination); + + assertEquals(expectedList, destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemoUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemoUnitTest.java new file mode 100644 index 0000000000..9978b57ca1 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingConstructorDemoUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.indexoutofbounds; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CopyListUsingConstructorDemoUnitTest { + + @Test + void whenCopyListUsingConstructor_thenMakeACopyOfList() { + List source = Arrays.asList(11, 22, 33); + + assertEquals(source, CopyListUsingConstructorDemo.copyList(source)); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemoUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemoUnitTest.java new file mode 100644 index 0000000000..1d541b03fa --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/CopyListUsingJava8StreamDemoUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.indexoutofbounds; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CopyListUsingJava8StreamDemoUnitTest { + + @Test + void whenCopyListUsingStream_thenMakeACopyOfArrayList() { + List source = Arrays.asList(11, 22, 33); + + assertEquals(source, CopyListUsingJava8StreamDemo.copyList(source)); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemoUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemoUnitTest.java new file mode 100644 index 0000000000..6a53ce8dfc --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/indexoutofbounds/IndexOutOfBoundsExceptionDemoUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.indexoutofbounds; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +class IndexOutOfBoundsExceptionDemoUnitTest { + + @Test + void givenDestinationArrayListSizeIsZero_whenCopySourceArrayListToDestination_thenShouldThrowIndexOutOfBoundsException() { + List source = Arrays.asList(1, 2, 3, 4, 5); + + assertThrows(IndexOutOfBoundsException.class, () -> IndexOutOfBoundsExceptionDemo.copyList(source)); + } + + @Test + void givenSourceAndDestinationListSizeIsEqual_whenCopySourceArrayListToDestination_thenShouldNotThrowIndexOutOfBoundsException() { + List source = Collections.emptyList(); + + assertEquals(source, IndexOutOfBoundsExceptionDemo.copyList(source)); + } +}