Skip to content

Commit 99bebe8

Browse files
authored
Refactor flattening lists example (eugenp#1372)
1 parent ac50687 commit 99bebe8

2 files changed

Lines changed: 40 additions & 45 deletions

File tree

‎core-java/src/main/java/com/baeldung/list/flattennestedlist/FlattenNestedList.java‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
11
package com.baeldung.list.flattennestedlist;
22

3-
import static org.junit.Assert.assertNotNull;
4-
import static org.junit.Assert.assertTrue;
3+
import org.junit.Test;
54

65
import java.util.ArrayList;
76
import java.util.Arrays;
7+
import java.util.Collection;
88
import java.util.List;
9+
import java.util.stream.Collectors;
910

10-
import org.junit.Before;
11-
import org.junit.Test;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertTrue;
1413

1514
public class FlattenNestedListTest {
1615

17-
private static final Logger LOGGER = LoggerFactory.getLogger(FlattenNestedListTest.class);
18-
private FlattenNestedList flol;
19-
20-
@Before
21-
public void setup() {
22-
flol = new FlattenNestedList();
23-
}
24-
2516
@Test
26-
public void givenListOfListOfString_flattenNestedList() {
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");
2722

28-
// create the list to flatten
23+
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
24+
25+
// when
26+
List<String> ls = flattenListOfListsImperatively(list);
27+
28+
// then
29+
assertNotNull(ls);
30+
assertTrue(ls.size() == 9);
31+
//TODO content assertion
32+
}
33+
34+
@Test
35+
public void givenListOfListOfString_flattenNestedList2() {
36+
// given
2937
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
3038
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
3139
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
3240

33-
List<List<String>> lol = new ArrayList<>();
34-
lol.addAll(Arrays.asList(ls1, ls2, ls3));
35-
36-
// show nested list
37-
LOGGER.debug("\nNested list: ");
38-
lol.forEach((nl) -> LOGGER.debug(nl + ""));
41+
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
3942

40-
// flatten it
41-
List<String> ls = flol.flattenListOfLists(lol);
43+
// when
44+
List<String> ls = flattenListOfListsStream(list);
4245

46+
// then
4347
assertNotNull(ls);
4448
assertTrue(ls.size() == 9);
45-
46-
// show flattened list
47-
LOGGER.debug("\nFlattened list:");
48-
ls.forEach((l) -> LOGGER.debug(l));
49+
//TODO content assertion
50+
}
51+
52+
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
53+
List<T> ls = new ArrayList<>();
54+
list.forEach(ls::addAll);
55+
return ls;
56+
}
4957

58+
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
59+
return list.stream()
60+
.flatMap(Collection::stream)
61+
.collect(Collectors.toList());
5062
}
5163

5264
}

0 commit comments

Comments
 (0)