展平数组测试

This commit is contained in:
YuCheng Hu 2018-12-27 13:35:36 -05:00
parent d12dd975c0
commit edd772d01c
2 changed files with 64 additions and 0 deletions

View File

@ -49,6 +49,13 @@
<version>4.12</version>
</dependency>
<!-- EMAIL -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
<!-- APACHE COMMONS -->
<dependency>

View File

@ -0,0 +1,57 @@
package com.ossez.codebank.interview.tests;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ossez.codebank.interview.KayakCountUpDown;
import com.ossez.codebank.interview.KayakRobotMovement;
/**
* PillPack
*
* <pre>
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
* </pre>
*
* @author YuCheng
*
*/
public class PillPackTest {
private final static Logger logger = LoggerFactory.getLogger(PillPackTest.class);
List<Integer> returnList = new ArrayList<Integer>();
/**
* https://www.cwiki.us/display/ITCLASSIFICATION/Flatten+Nested+Arrays
*
* FlattenNestedArrays
*/
@Test
public void testFlattenNestedArrays() {
logger.debug("Test FlattenNestedArrays");
Object[] array = { 1, 2, new Object[] { 3, 4, new Object[] { 5 }, 6, 7 }, 8, 9, 10 };
logger.debug("{} - > {}", Arrays.deepToString(array), Arrays.toString(java8Flatten(array).toArray()));
}
/**
* Java 8 Stream to Flatten array.
*
* @param array
* @return
*/
private static Stream<Object> java8Flatten(Object[] array) {
return Arrays.stream(array).flatMap(o -> o instanceof Object[] ? java8Flatten((Object[]) o) : Stream.of(o));
}
}