commit
3888cd0af1
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.exception.indexoutofbounds;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CopyListUsingAddAllMethodDemo {
|
||||
static List<Integer> copyList(List<Integer> source) {
|
||||
List<Integer> destination = new ArrayList<>();
|
||||
|
||||
destination.addAll(source);
|
||||
|
||||
return destination;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.exception.indexoutofbounds;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CopyListUsingCollectionsCopyMethodDemo {
|
||||
static void copyList(List<Integer> source, List<Integer> destination) {
|
||||
Collections.copy(destination, source);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.exception.indexoutofbounds;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CopyListUsingConstructorDemo {
|
||||
static List<Integer> copyList(List<Integer> source) {
|
||||
return new ArrayList<>(source);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.exception.indexoutofbounds;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.exception.indexoutofbounds;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class IndexOutOfBoundsExceptionDemo {
|
||||
static List<Integer> copyList(List<Integer> source) {
|
||||
List<Integer> destination = new ArrayList<>(source.size());
|
||||
Collections.copy(destination, source);
|
||||
return destination;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.exception.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<Integer> source = Arrays.asList(11, 22, 33);
|
||||
|
||||
assertEquals(source, CopyListUsingAddAllMethodDemo.copyList(source));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.exception.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<Integer> source = Arrays.asList(11, 22, 33);
|
||||
List<Integer> destination = Arrays.asList(1, 2, 3);
|
||||
|
||||
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
|
||||
|
||||
assertEquals(source, destination);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCopyListUsingCollectionsCopy_thenOverrideInitialDestinationValuesAndOthersShouldBeUnchanged(){
|
||||
List<Integer> source = Arrays.asList(11, 22, 33);
|
||||
List<Integer> destination = Arrays.asList(1, 2, 3, 4, 5);
|
||||
List<Integer> expectedList = Arrays.asList(11, 22, 33, 4, 5);
|
||||
|
||||
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
|
||||
|
||||
assertEquals(expectedList, destination);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.exception.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<Integer> source = Arrays.asList(11, 22, 33);
|
||||
|
||||
assertEquals(source, CopyListUsingConstructorDemo.copyList(source));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.exception.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<Integer> source = Arrays.asList(11, 22, 33);
|
||||
|
||||
assertEquals(source, CopyListUsingJava8StreamDemo.copyList(source));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.exception.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<Integer> source = Arrays.asList(1, 2, 3, 4, 5);
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> IndexOutOfBoundsExceptionDemo.copyList(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSourceAndDestinationListSizeIsEqual_whenCopySourceArrayListToDestination_thenShouldNotThrowIndexOutOfBoundsException() {
|
||||
List<Integer> source = Collections.emptyList();
|
||||
|
||||
assertEquals(source, IndexOutOfBoundsExceptionDemo.copyList(source));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue