Add examples to demo IndexOutOfBoundsException while using Collectins.copy method and other working demos to copy elements from one list to another list

This commit is contained in:
Vishal 2020-09-13 23:05:36 +05:30
parent 760ba574e4
commit c6f067c17f
6 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CopyListUsingAddAllMethodDemo {
static List<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>();
destination.addAll(source);
return destination;
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = copyList(source);
System.out.println("copy = " + destination);
}
}

View File

@ -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<Integer> source, List<Integer> destination) {
Collections.copy(destination, source);
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = Arrays.asList(1, 2, 3, 4, 5);
copyList(source, destination);
System.out.println("copy = " + destination);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CopyListUsingConstructorDemo {
static List<Integer> copyList(List<Integer> source) {
return new ArrayList<>(source);
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = copyList(source);
System.out.println("copy = " + destination);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung;
import java.util.Arrays;
import java.util.List;
public class CopyListUsingJava10Demo {
static List<Integer> copyList(List<Integer> source) {
return List.copyOf(source);
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = copyList(source);
System.out.println("copy = " + destination);
}
}

View File

@ -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<Integer> copyList(List<Integer> source) {
return source
.stream()
.collect(Collectors.toList());
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = copyList(source);
System.out.println("copy = " + destination);
}
}

View File

@ -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<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>(source.size());
Collections.copy(destination, source);
return destination;
}
public static void main(String[] args) {
List<Integer> source = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> copy = copyList(source);
System.out.println("copy = " + copy);
}
}