|
1 | 1 | package com.baeldung.list.flattennestedlist; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertNotNull; |
4 | | -import static org.junit.Assert.assertTrue; |
| 3 | +import org.junit.Test; |
5 | 4 |
|
6 | 5 | import java.util.ArrayList; |
7 | 6 | import java.util.Arrays; |
| 7 | +import java.util.Collection; |
8 | 8 | import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
9 | 10 |
|
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; |
14 | 13 |
|
15 | 14 | public class FlattenNestedListTest { |
16 | 15 |
|
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 | | - |
25 | 16 | @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"); |
27 | 22 |
|
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 |
29 | 37 | List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three"); |
30 | 38 | List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three"); |
31 | 39 | List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three"); |
32 | 40 |
|
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); |
39 | 42 |
|
40 | | - // flatten it |
41 | | - List<String> ls = flol.flattenListOfLists(lol); |
| 43 | + // when |
| 44 | + List<String> ls = flattenListOfListsStream(list); |
42 | 45 |
|
| 46 | + // then |
43 | 47 | assertNotNull(ls); |
44 | 48 | 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 | + } |
49 | 57 |
|
| 58 | + public <T> List<T> flattenListOfListsStream(List<List<T>> list) { |
| 59 | + return list.stream() |
| 60 | + .flatMap(Collection::stream) |
| 61 | + .collect(Collectors.toList()); |
50 | 62 | } |
51 | 63 |
|
52 | 64 | } |
0 commit comments