Refactor flattening lists example (#1372)
This commit is contained in:
parent
ac50687880
commit
99bebe806c
|
@ -1,17 +0,0 @@
|
|||
package com.baeldung.list.flattennestedlist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FlattenNestedList {
|
||||
|
||||
public List<String> flattenListOfLists(List<List<String>> lol) {
|
||||
|
||||
// flatten the list
|
||||
List<String> ls = new ArrayList<>();
|
||||
lol.forEach((k) -> ls.addAll(k));
|
||||
|
||||
return ls;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,52 +1,64 @@
|
|||
package com.baeldung.list.flattennestedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FlattenNestedListTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlattenNestedListTest.class);
|
||||
private FlattenNestedList flol;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
flol = new FlattenNestedList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfListOfString_flattenNestedList() {
|
||||
|
||||
// create the list to flatten
|
||||
public void givenListOfListOfString_flattenNestedList1() {
|
||||
// given
|
||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||
|
||||
List<List<String>> lol = new ArrayList<>();
|
||||
lol.addAll(Arrays.asList(ls1, ls2, ls3));
|
||||
|
||||
// show nested list
|
||||
LOGGER.debug("\nNested list: ");
|
||||
lol.forEach((nl) -> LOGGER.debug(nl + ""));
|
||||
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||
|
||||
// flatten it
|
||||
List<String> ls = flol.flattenListOfLists(lol);
|
||||
// when
|
||||
List<String> ls = flattenListOfListsImperatively(list);
|
||||
|
||||
// then
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
|
||||
// show flattened list
|
||||
LOGGER.debug("\nFlattened list:");
|
||||
ls.forEach((l) -> LOGGER.debug(l));
|
||||
//TODO content assertion
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfListOfString_flattenNestedList2() {
|
||||
// given
|
||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||
|
||||
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||
|
||||
// when
|
||||
List<String> ls = flattenListOfListsStream(list);
|
||||
|
||||
// then
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
//TODO content assertion
|
||||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||
List<T> ls = new ArrayList<>();
|
||||
list.forEach(ls::addAll);
|
||||
return ls;
|
||||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||
return list.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue