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
0
core-java/0.8260098203820962
Normal file
0
core-java/0.8260098203820962
Normal file
@ -1,6 +1,8 @@
|
|||||||
package com.baeldung.list.flattennestedlist;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -8,45 +10,60 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import org.junit.After;
|
||||||
import static org.junit.Assert.assertTrue;
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class FlattenNestedListTest {
|
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
|
@Before
|
||||||
public void givenListOfListOfString_flattenNestedList1() {
|
public void setup() {
|
||||||
// given
|
lol.addAll(Arrays.asList(ls1, ls2, ls3));
|
||||||
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);
|
@After
|
||||||
|
public void tearDown() {
|
||||||
// when
|
lol = null;
|
||||||
List<String> ls = flattenListOfListsImperatively(list);
|
|
||||||
|
|
||||||
// then
|
|
||||||
assertNotNull(ls);
|
|
||||||
assertTrue(ls.size() == 9);
|
|
||||||
//TODO content assertion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListOfListOfString_flattenNestedList2() {
|
public void givenString_flattenNestedList1() {
|
||||||
// given
|
List<String> ls = flattenListOfListsImperatively(lol);
|
||||||
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);
|
assertNotNull(ls);
|
||||||
assertTrue(ls.size() == 9);
|
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) {
|
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) {
|
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||||
return list.stream()
|
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
|
||||||
.flatMap(Collection::stream)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user