Integrate forEach and stream changes to test, removing non-test class. (#1529)
* article Bael-667 initial commit. * Switch to use logging framework for output.
This commit is contained in:
parent
b63ef448db
commit
ea345fa246
|
@ -1,6 +1,8 @@
|
|||
package com.baeldung.list.flattennestedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -8,45 +10,60 @@ 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 org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FlattenNestedListTest {
|
||||
private List<List<String>> lol = new ArrayList<>();
|
||||
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");
|
||||
|
||||
@Test
|
||||
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");
|
||||
@Before
|
||||
public void setup() {
|
||||
lol.addAll(Arrays.asList(ls1, ls2, ls3));
|
||||
}
|
||||
|
||||
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||
|
||||
// when
|
||||
List<String> ls = flattenListOfListsImperatively(list);
|
||||
|
||||
// then
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
//TODO content assertion
|
||||
@After
|
||||
public void tearDown() {
|
||||
lol = null;
|
||||
}
|
||||
|
||||
@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");
|
||||
public void givenString_flattenNestedList1() {
|
||||
List<String> ls = flattenListOfListsImperatively(lol);
|
||||
|
||||
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
|
||||
// assert content
|
||||
assertEquals(ls.get(0), "one:one");
|
||||
assertEquals(ls.get(1), "one:two");
|
||||
assertEquals(ls.get(2), "one:three");
|
||||
assertEquals(ls.get(3), "two:one");
|
||||
assertEquals(ls.get(4), "two:two");
|
||||
assertEquals(ls.get(5), "two:three");
|
||||
assertEquals(ls.get(6), "three:one");
|
||||
assertEquals(ls.get(7), "three:two");
|
||||
assertEquals(ls.get(8), "three:three");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_flattenNestedList2() {
|
||||
List<String> ls = flattenListOfListsStream(lol);
|
||||
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
// assert content
|
||||
assertEquals(ls.get(0), "one:one");
|
||||
assertEquals(ls.get(1), "one:two");
|
||||
assertEquals(ls.get(2), "one:three");
|
||||
assertEquals(ls.get(3), "two:one");
|
||||
assertEquals(ls.get(4), "two:two");
|
||||
assertEquals(ls.get(5), "two:three");
|
||||
assertEquals(ls.get(6), "three:one");
|
||||
assertEquals(ls.get(7), "three:two");
|
||||
assertEquals(ls.get(8), "three:three");
|
||||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||
|
@ -56,9 +73,6 @@ public class FlattenNestedListTest {
|
|||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||
return list.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue