Skip to content

Commit 748c94f

Browse files
fscorromaibin
authored andcommitted
Add items to list in core-java-collections (eugenp#4841)
1 parent 81d9417 commit 748c94f

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.java.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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.list.listoflist;
2+
3+
import com.baeldung.java.list.Flower;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import java.util.*;
8+
import static org.junit.Assert.*;
9+
10+
public class AddElementsToListUnitTest {
11+
12+
List<Flower> flowers;
13+
14+
@Before
15+
public void init() {
16+
this.flowers = new ArrayList<>(Arrays.asList(
17+
new Flower("Poppy", 12),
18+
new Flower("Anemone", 8),
19+
new Flower("Catmint", 12)));
20+
}
21+
@Test
22+
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
23+
List<Flower> anotherList = new ArrayList<>();
24+
anotherList.addAll(flowers);
25+
assertEquals(anotherList.size(), flowers.size());
26+
Assert.assertTrue(anotherList.containsAll(flowers));
27+
}
28+
@Test
29+
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
30+
List<Flower> anotherList = new ArrayList<>();
31+
anotherList.addAll(flowers);
32+
Flower flower = anotherList.get(0);
33+
flower.setPetals(flowers.get(0).getPetals() * 3);
34+
assertEquals(anotherList.size(), flowers.size());
35+
Assert.assertTrue(anotherList.containsAll(flowers));
36+
}
37+
@Test
38+
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
39+
List<Flower> target = new ArrayList<>();
40+
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
41+
assertEquals(target.size(), 4);
42+
}
43+
@Test
44+
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
45+
List<Flower> flowerVase = new ArrayList<>();
46+
flowers.stream()
47+
.skip(1)
48+
.forEachOrdered(flowerVase::add);
49+
assertEquals(flowerVase.size() + 1, flowers.size());
50+
assertFalse(flowerVase.containsAll(flowers));
51+
}
52+
@Test
53+
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
54+
List<Flower> flowerVase = new ArrayList<>();
55+
flowers.stream()
56+
.filter(f -> f.getPetals() > 10)
57+
.forEachOrdered(flowerVase::add);
58+
assertEquals(flowerVase.size() + 1, flowers.size());
59+
assertFalse(flowerVase.containsAll(flowers));
60+
}
61+
@Test
62+
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
63+
List<Flower> target = new ArrayList<>();
64+
Optional.ofNullable(flowers)
65+
.ifPresent(target::addAll);
66+
assertNotNull(target);
67+
assertEquals(target.size(), 3);
68+
}
69+
}

0 commit comments

Comments
 (0)