Skip to content

Commit ea345fa

Browse files
gitterjim-Izhendrikse
authored andcommitted
Integrate forEach and stream changes to test, removing non-test class. (eugenp#1529)
* article Bael-667 initial commit. * Switch to use logging framework for output.
1 parent b63ef44 commit ea345fa

2 files changed

Lines changed: 44 additions & 30 deletions

File tree

‎core-java/0.8260098203820962‎

Whitespace-only changes.
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,69 @@
11
package com.baeldung.list.flattennestedlist;
22

3-
import org.junit.Test;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
46

57
import java.util.ArrayList;
68
import java.util.Arrays;
79
import java.util.Collection;
810
import java.util.List;
911
import java.util.stream.Collectors;
1012

11-
import static org.junit.Assert.assertNotNull;
12-
import static org.junit.Assert.assertTrue;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
1316

1417
public class FlattenNestedListTest {
18+
private List<List<String>> lol = new ArrayList<>();
19+
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
20+
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
21+
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
1522

16-
@Test
17-
public void givenListOfListOfString_flattenNestedList1() {
18-
// given
19-
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
20-
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
21-
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
23+
@Before
24+
public void setup() {
25+
lol.addAll(Arrays.asList(ls1, ls2, ls3));
26+
}
2227

23-
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
28+
@After
29+
public void tearDown() {
30+
lol = null;
31+
}
2432

25-
// when
26-
List<String> ls = flattenListOfListsImperatively(list);
33+
@Test
34+
public void givenString_flattenNestedList1() {
35+
List<String> ls = flattenListOfListsImperatively(lol);
2736

28-
// then
2937
assertNotNull(ls);
3038
assertTrue(ls.size() == 9);
31-
//TODO content assertion
39+
// assert content
40+
assertEquals(ls.get(0), "one:one");
41+
assertEquals(ls.get(1), "one:two");
42+
assertEquals(ls.get(2), "one:three");
43+
assertEquals(ls.get(3), "two:one");
44+
assertEquals(ls.get(4), "two:two");
45+
assertEquals(ls.get(5), "two:three");
46+
assertEquals(ls.get(6), "three:one");
47+
assertEquals(ls.get(7), "three:two");
48+
assertEquals(ls.get(8), "three:three");
3249
}
3350

3451
@Test
35-
public void givenListOfListOfString_flattenNestedList2() {
36-
// given
37-
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
38-
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
39-
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
52+
public void givenString_flattenNestedList2() {
53+
List<String> ls = flattenListOfListsStream(lol);
4054

41-
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
42-
43-
// when
44-
List<String> ls = flattenListOfListsStream(list);
45-
46-
// then
4755
assertNotNull(ls);
4856
assertTrue(ls.size() == 9);
49-
//TODO content assertion
57+
// assert content
58+
assertEquals(ls.get(0), "one:one");
59+
assertEquals(ls.get(1), "one:two");
60+
assertEquals(ls.get(2), "one:three");
61+
assertEquals(ls.get(3), "two:one");
62+
assertEquals(ls.get(4), "two:two");
63+
assertEquals(ls.get(5), "two:three");
64+
assertEquals(ls.get(6), "three:one");
65+
assertEquals(ls.get(7), "three:two");
66+
assertEquals(ls.get(8), "three:three");
5067
}
5168

5269
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
@@ -56,9 +73,6 @@ public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
5673
}
5774

5875
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
59-
return list.stream()
60-
.flatMap(Collection::stream)
61-
.collect(Collectors.toList());
76+
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
6277
}
63-
6478
}

0 commit comments

Comments
 (0)