Created
September 19, 2019 10:04
-
-
Save seyijava/7d705b6d9f2887e9bc08da08535d18d3 to your computer and use it in GitHub Desktop.
Helloooo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bigdataconcept.algo.util; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import static java.util.Arrays.asList; | |
/** | |
* @author Oluwaseyi otun | |
* | |
*/ | |
public class FlattenArrayUtil | |
{ | |
public static void main( String[] args ) | |
{ | |
List<Object> numberArrayList = array(array(1,2,array(3)),array(4)); | |
List<Object> flattenArrayList = flattenArray(numberArrayList); | |
System.out.println(numberArrayList); | |
System.out.println("flatten Array: " + flattenArrayList); | |
} | |
private static List<Object> array(Object... number) { | |
return asList(number); | |
} | |
private static Stream<Object> flattenArrayToStream(List<?> list) { | |
return list.stream().flatMap(item -> | |
item instanceof List<?> ? | |
flattenArrayToStream((List<?>)item) : | |
Stream.of(item)); | |
} | |
private static List<Object> flattenArray(List<?> list) { | |
return flattenArrayToStream(list).collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment