Skip to content

Commit f68615d

Browse files
fscorromaibin
authored andcommitted
Copy list to another list examples (eugenp#4725)
1 parent 61e6d9a commit f68615d

4 files changed

Lines changed: 244 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.java10.list;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
public class CopyListServiceUnitTest {
9+
10+
@Test(expected = UnsupportedOperationException.class)
11+
public void whenModifyCopyOfList_thenThrowsException() {
12+
List<Integer> copyList = List.copyOf(Arrays.asList(1, 2, 3, 4));
13+
}
14+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.list;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Optional;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public class CopyListService {
11+
12+
public List<Flower> copyListByConstructor(List<Flower> source) {
13+
return new ArrayList<Flower>(source);
14+
}
15+
16+
public List<Flower> copyListByConstructorAndEditOneFlowerInTheNewList(List<Flower> source) {
17+
List<Flower> flowers = new ArrayList<>(source);
18+
if(flowers.size() > 0) {
19+
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
20+
}
21+
22+
return flowers;
23+
}
24+
25+
public List<Flower> copyListByAddAllMethod(List<Flower> source) {
26+
List<Flower> flowers = new ArrayList<>();
27+
flowers.addAll(source);
28+
return flowers;
29+
}
30+
31+
public List<Flower> copyListByAddAllMethodAndEditOneFlowerInTheNewList(List<Flower> source) {
32+
List<Flower> flowers = new ArrayList<>();
33+
flowers.addAll(source);
34+
35+
if(flowers.size() > 0) {
36+
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
37+
}
38+
39+
return flowers;
40+
}
41+
42+
public List<Integer> copyListByCopyMethod(List<Integer> source, List<Integer> dest) {
43+
Collections.copy(dest, source);
44+
return dest;
45+
}
46+
47+
public List<Flower> copyListByStream(List<Flower> source) {
48+
return source.stream().collect(Collectors.toList());
49+
}
50+
51+
public List<Flower> copyListByStreamAndSkipFirstElement(List<Flower> source) {
52+
return source.stream().skip(1).collect(Collectors.toList());
53+
}
54+
55+
public List<Flower> copyListByStreamWithFilter(List<Flower> source, Integer moreThanPetals) {
56+
return source.stream().filter(f -> f.getPetals() > moreThanPetals).collect(Collectors.toList());
57+
}
58+
59+
public List<Flower> copyListByStreamWithOptional(List<Flower> source) {
60+
return Optional.ofNullable(source)
61+
.map(List::stream)
62+
.orElseGet(Stream::empty)
63+
.collect(Collectors.toList());
64+
}
65+
66+
public List<Flower> copyListByStreamWithOptionalAndSkip(List<Flower> source) {
67+
return Optional.ofNullable(source)
68+
.map(List::stream)
69+
.orElseGet(Stream::empty)
70+
.skip(1)
71+
.collect(Collectors.toList());
72+
}
73+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.list;
2+
3+
public class Flower {
4+
5+
private String name;
6+
private int petals;
7+
8+
public Flower(String name, int petals) {
9+
this.name = name;
10+
this.petals = petals;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public int getPetals() {
22+
return petals;
23+
}
24+
25+
public void setPetals(int petals) {
26+
this.petals = petals;
27+
}
28+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.baeldung.list;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class CopyListServiceUnitTest {
13+
14+
List<Flower> flowers;
15+
16+
private CopyListService copyListService;
17+
18+
@Before
19+
public void init() {
20+
this.copyListService = new CopyListService();
21+
this.flowers = new ArrayList<>();
22+
23+
Flower poppy = new Flower("Poppy", 12);
24+
flowers.add(poppy);
25+
Flower anemone = new Flower("Anemone", 8);
26+
flowers.add(anemone);
27+
Flower catmint = new Flower("Catmint", 12);
28+
flowers.add(catmint);
29+
Flower diascia = new Flower("Diascia", 5);
30+
flowers.add(diascia);
31+
Flower iris = new Flower("Iris", 3);
32+
flowers.add(iris);
33+
Flower pansy = new Flower("Pansy", 5);
34+
flowers.add(pansy);
35+
}
36+
37+
@Test
38+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByConstructor() {
39+
List<Flower> copy = copyListService.copyListByConstructor(flowers);
40+
assertEquals(copy.size(), flowers.size());
41+
assertTrue(copy.containsAll(flowers));
42+
}
43+
44+
@Test
45+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByConstructor() {
46+
List<Flower> copy = copyListService.copyListByConstructorAndEditOneFlowerInTheNewList(flowers);
47+
assertEquals(copy.size(), flowers.size());
48+
assertTrue(copy.containsAll(flowers));
49+
}
50+
51+
@Test
52+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByAddAllmethod() {
53+
List<Flower> copy = copyListService.copyListByAddAllMethod(flowers);
54+
assertEquals(copy.size(), flowers.size());
55+
assertTrue(copy.containsAll(flowers));
56+
}
57+
58+
@Test
59+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByAddAllmethod() {
60+
List<Flower> copy = copyListService.copyListByAddAllMethodAndEditOneFlowerInTheNewList(flowers);
61+
assertEquals(copy.size(), flowers.size());
62+
assertTrue(copy.containsAll(flowers));
63+
}
64+
65+
@Test
66+
public void givenAList_whenListsHaveSameSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
67+
List<Integer> source = Arrays.asList(1,2,3);
68+
List<Integer> dest = Arrays.asList(4,5,6);
69+
70+
dest = copyListService.copyListByCopyMethod(source, dest);
71+
assertEquals(dest.size(), source.size());
72+
assertTrue(dest.containsAll(source));
73+
}
74+
75+
@Test
76+
public void givenAList_whenListsHaveDifferentSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
77+
List<Integer> source = Arrays.asList(1,2,3);
78+
List<Integer> dest = Arrays.asList(5,6,7,8,9,10);
79+
80+
dest = copyListService.copyListByCopyMethod(source, dest);
81+
assertNotEquals(dest.size(), source.size());
82+
assertTrue(dest.containsAll(source));
83+
}
84+
85+
@Test
86+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByStreamProcess() {
87+
List<Flower> copy = copyListService.copyListByStream(flowers);
88+
assertEquals(copy.size(), flowers.size());
89+
assertTrue(copy.containsAll(flowers));
90+
}
91+
92+
@Test
93+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneElementLessByStreamProcess() {
94+
List<Flower> copy = copyListService.copyListByStreamAndSkipFirstElement(flowers);
95+
assertNotEquals(copy.size(), flowers.size());
96+
assertEquals(copy.size() + 1, flowers.size());
97+
assertFalse(copy.containsAll(flowers));
98+
}
99+
100+
@Test
101+
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithFilterElementsByStreamProcess() {
102+
List<Flower> copy = copyListService.copyListByStreamWithFilter(flowers, 5);
103+
assertNotEquals(copy.size(), flowers.size());
104+
assertEquals(copy.size() + 3, flowers.size());
105+
assertFalse(copy.containsAll(flowers));
106+
}
107+
108+
@Test
109+
public void givenAList_whenListIsNull_thenReturnEmptyListByStreamProcess() {
110+
List<Flower> copy = copyListService.copyListByStreamWithOptional(null);
111+
assertNotNull(copy);
112+
assertEquals(copy.size(), 0);
113+
}
114+
115+
@Test
116+
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithTheElementsByStreamProcess() {
117+
List<Flower> copy = copyListService.copyListByStreamWithOptional(flowers);
118+
assertEquals(copy.size(), flowers.size());
119+
assertTrue(copy.containsAll(flowers));
120+
}
121+
122+
@Test
123+
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithOneElementLessByStreamProcess() {
124+
List<Flower> copy = copyListService.copyListByStreamWithOptionalAndSkip(flowers);
125+
assertNotEquals(copy.size(), flowers.size());
126+
assertEquals(copy.size() + 1, flowers.size());
127+
assertFalse(copy.containsAll(flowers));
128+
}
129+
}

0 commit comments

Comments
 (0)