From c6f067c17f2351f845dab5bb9f83bcb6f1bf039b Mon Sep 17 00:00:00 2001 From: Vishal Date: Sun, 13 Sep 2020 23:05:36 +0530 Subject: [PATCH] Add examples to demo IndexOutOfBoundsException while using Collectins.copy method and other working demos to copy elements from one list to another list --- .../CopyListUsingAddAllMethodDemo.java | 24 +++++++++++++++++ ...opyListUsingCollectionsCopyMethodDemo.java | 21 +++++++++++++++ .../CopyListUsingConstructorDemo.java | 21 +++++++++++++++ .../com/baeldung/CopyListUsingJava10Demo.java | 18 +++++++++++++ .../CopyListUsingJava8StreamDemo.java | 22 +++++++++++++++ .../exception/IndexOutOfBoundsException.java | 27 +++++++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingAddAllMethodDemo.java create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingCollectionsCopyMethodDemo.java create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingConstructorDemo.java create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava10Demo.java create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava8StreamDemo.java create mode 100644 core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exception/IndexOutOfBoundsException.java diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingAddAllMethodDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingAddAllMethodDemo.java new file mode 100644 index 0000000000..a7d24cdec8 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingAddAllMethodDemo.java @@ -0,0 +1,24 @@ +package com.baeldung; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CopyListUsingAddAllMethodDemo { + + static List copyList(List source) { + List destination = new ArrayList<>(); + + destination.addAll(source); + + return destination; + } + + public static void main(String[] args) { + List source = Arrays.asList(11, 22, 33); + + List destination = copyList(source); + + System.out.println("copy = " + destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingCollectionsCopyMethodDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingCollectionsCopyMethodDemo.java new file mode 100644 index 0000000000..bdf6726924 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingCollectionsCopyMethodDemo.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class CopyListUsingCollectionsCopyMethodDemo { + + static void copyList(List source, List destination) { + Collections.copy(destination, source); + } + + public static void main(String[] args) { + List source = Arrays.asList(11, 22, 33); + List destination = Arrays.asList(1, 2, 3, 4, 5); + + copyList(source, destination); + + System.out.println("copy = " + destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingConstructorDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingConstructorDemo.java new file mode 100644 index 0000000000..b76a1448d4 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingConstructorDemo.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CopyListUsingConstructorDemo { + + static List copyList(List source) { + + return new ArrayList<>(source); + } + + public static void main(String[] args) { + List source = Arrays.asList(11, 22, 33); + + List destination = copyList(source); + + System.out.println("copy = " + destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava10Demo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava10Demo.java new file mode 100644 index 0000000000..1a32dcec4f --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava10Demo.java @@ -0,0 +1,18 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; + +public class CopyListUsingJava10Demo { + static List copyList(List source) { + return List.copyOf(source); + } + + public static void main(String[] args) { + List source = Arrays.asList(11, 22, 33); + + List destination = copyList(source); + + System.out.println("copy = " + destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava8StreamDemo.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava8StreamDemo.java new file mode 100644 index 0000000000..81ae533505 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/CopyListUsingJava8StreamDemo.java @@ -0,0 +1,22 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class CopyListUsingJava8StreamDemo { + + static List copyList(List source) { + return source + .stream() + .collect(Collectors.toList()); + } + + public static void main(String[] args) { + List source = Arrays.asList(11, 22, 33); + + List destination = copyList(source); + + System.out.println("copy = " + destination); + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exception/IndexOutOfBoundsException.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exception/IndexOutOfBoundsException.java new file mode 100644 index 0000000000..a1b6bf1151 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exception/IndexOutOfBoundsException.java @@ -0,0 +1,27 @@ +package com.baeldung.exception; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * This example produces an IndexOutOfBoundsException, when we try to copy a list using the Collections.copy method. + * As the destination list doesn't have enough space/size to copy elements from source list. + */ +public class IndexOutOfBoundsException { + + static List copyList(List source) { + List destination = new ArrayList<>(source.size()); + Collections.copy(destination, source); + return destination; + } + + public static void main(String[] args) { + List source = Arrays.asList(1, 2, 3, 4, 5); + List copy = copyList(source); + + System.out.println("copy = " + copy); + } + +}