Copy list to another list examples (#4725)

This commit is contained in:
Felipe Santiago Corro 2018-07-16 03:41:08 -03:00 committed by maibin
parent 61e6d9a3cd
commit f68615d772
4 changed files with 244 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.java10.list;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class CopyListServiceUnitTest {
@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
List<Integer> copyList = List.copyOf(Arrays.asList(1, 2, 3, 4));
}
}

View File

@ -0,0 +1,73 @@
package com.baeldung.list;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CopyListService {
public List<Flower> copyListByConstructor(List<Flower> source) {
return new ArrayList<Flower>(source);
}
public List<Flower> copyListByConstructorAndEditOneFlowerInTheNewList(List<Flower> source) {
List<Flower> flowers = new ArrayList<>(source);
if(flowers.size() > 0) {
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
}
return flowers;
}
public List<Flower> copyListByAddAllMethod(List<Flower> source) {
List<Flower> flowers = new ArrayList<>();
flowers.addAll(source);
return flowers;
}
public List<Flower> copyListByAddAllMethodAndEditOneFlowerInTheNewList(List<Flower> source) {
List<Flower> flowers = new ArrayList<>();
flowers.addAll(source);
if(flowers.size() > 0) {
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
}
return flowers;
}
public List<Integer> copyListByCopyMethod(List<Integer> source, List<Integer> dest) {
Collections.copy(dest, source);
return dest;
}
public List<Flower> copyListByStream(List<Flower> source) {
return source.stream().collect(Collectors.toList());
}
public List<Flower> copyListByStreamAndSkipFirstElement(List<Flower> source) {
return source.stream().skip(1).collect(Collectors.toList());
}
public List<Flower> copyListByStreamWithFilter(List<Flower> source, Integer moreThanPetals) {
return source.stream().filter(f -> f.getPetals() > moreThanPetals).collect(Collectors.toList());
}
public List<Flower> copyListByStreamWithOptional(List<Flower> source) {
return Optional.ofNullable(source)
.map(List::stream)
.orElseGet(Stream::empty)
.collect(Collectors.toList());
}
public List<Flower> copyListByStreamWithOptionalAndSkip(List<Flower> source) {
return Optional.ofNullable(source)
.map(List::stream)
.orElseGet(Stream::empty)
.skip(1)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.list;
public class Flower {
private String name;
private int petals;
public Flower(String name, int petals) {
this.name = name;
this.petals = petals;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPetals() {
return petals;
}
public void setPetals(int petals) {
this.petals = petals;
}
}

View File

@ -0,0 +1,129 @@
package com.baeldung.list;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class CopyListServiceUnitTest {
List<Flower> flowers;
private CopyListService copyListService;
@Before
public void init() {
this.copyListService = new CopyListService();
this.flowers = new ArrayList<>();
Flower poppy = new Flower("Poppy", 12);
flowers.add(poppy);
Flower anemone = new Flower("Anemone", 8);
flowers.add(anemone);
Flower catmint = new Flower("Catmint", 12);
flowers.add(catmint);
Flower diascia = new Flower("Diascia", 5);
flowers.add(diascia);
Flower iris = new Flower("Iris", 3);
flowers.add(iris);
Flower pansy = new Flower("Pansy", 5);
flowers.add(pansy);
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByConstructor() {
List<Flower> copy = copyListService.copyListByConstructor(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByConstructor() {
List<Flower> copy = copyListService.copyListByConstructorAndEditOneFlowerInTheNewList(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByAddAllmethod() {
List<Flower> copy = copyListService.copyListByAddAllMethod(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByAddAllmethod() {
List<Flower> copy = copyListService.copyListByAddAllMethodAndEditOneFlowerInTheNewList(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListsHaveSameSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
List<Integer> source = Arrays.asList(1,2,3);
List<Integer> dest = Arrays.asList(4,5,6);
dest = copyListService.copyListByCopyMethod(source, dest);
assertEquals(dest.size(), source.size());
assertTrue(dest.containsAll(source));
}
@Test
public void givenAList_whenListsHaveDifferentSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
List<Integer> source = Arrays.asList(1,2,3);
List<Integer> dest = Arrays.asList(5,6,7,8,9,10);
dest = copyListService.copyListByCopyMethod(source, dest);
assertNotEquals(dest.size(), source.size());
assertTrue(dest.containsAll(source));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByStreamProcess() {
List<Flower> copy = copyListService.copyListByStream(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneElementLessByStreamProcess() {
List<Flower> copy = copyListService.copyListByStreamAndSkipFirstElement(flowers);
assertNotEquals(copy.size(), flowers.size());
assertEquals(copy.size() + 1, flowers.size());
assertFalse(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithFilterElementsByStreamProcess() {
List<Flower> copy = copyListService.copyListByStreamWithFilter(flowers, 5);
assertNotEquals(copy.size(), flowers.size());
assertEquals(copy.size() + 3, flowers.size());
assertFalse(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListIsNull_thenReturnEmptyListByStreamProcess() {
List<Flower> copy = copyListService.copyListByStreamWithOptional(null);
assertNotNull(copy);
assertEquals(copy.size(), 0);
}
@Test
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithTheElementsByStreamProcess() {
List<Flower> copy = copyListService.copyListByStreamWithOptional(flowers);
assertEquals(copy.size(), flowers.size());
assertTrue(copy.containsAll(flowers));
}
@Test
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithOneElementLessByStreamProcess() {
List<Flower> copy = copyListService.copyListByStreamWithOptionalAndSkip(flowers);
assertNotEquals(copy.size(), flowers.size());
assertEquals(copy.size() + 1, flowers.size());
assertFalse(copy.containsAll(flowers));
}
}