Latest review changes: more concise code and suggested refactoring. (#1549)
* article Bael-667 initial commit. * Switch to use logging framework for output. * Make code more concise. Refactor as suggested.
This commit is contained in:
parent
3c334e6b56
commit
aaf5a3e7b8
@ -1,51 +1,29 @@
|
|||||||
package com.baeldung.list.flattennestedlist;
|
package com.baeldung.list.flattennestedlist;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static java.util.Arrays.asList;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.hamcrest.collection.IsIterableContainingInOrder;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class FlattenNestedListTest {
|
public class FlattenNestedListTest {
|
||||||
private List<List<String>> lol = new ArrayList<>();
|
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
lol = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_flattenNestedList1() {
|
public void givenString_flattenNestedList1() {
|
||||||
List<String> ls = flattenListOfListsImperatively(lol);
|
List<String> ls = flattenListOfListsImperatively(lol);
|
||||||
|
|
||||||
assertNotNull(ls);
|
assertNotNull(ls);
|
||||||
assertTrue(ls.size() == 9);
|
assertTrue(ls.size() == 8);
|
||||||
// assert content
|
// assert content
|
||||||
assertEquals(ls.get(0), "one:one");
|
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
||||||
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
|
@Test
|
||||||
@ -53,17 +31,9 @@ public class FlattenNestedListTest {
|
|||||||
List<String> ls = flattenListOfListsStream(lol);
|
List<String> ls = flattenListOfListsStream(lol);
|
||||||
|
|
||||||
assertNotNull(ls);
|
assertNotNull(ls);
|
||||||
assertTrue(ls.size() == 9);
|
assertTrue(ls.size() == 8);
|
||||||
// assert content
|
// assert content
|
||||||
assertEquals(ls.get(0), "one:one");
|
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
||||||
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) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user