flattern Tree

flat a tree
 avatar
user_9487184
java
a year ago
622 B
1
Indexable
Never
public class Main {
    public static void main(String[] args) {
        var tree = List.of(
                List.of(1, 1, 2, List.of(2, 3, 4, List.of(99,900))),
                List.of(1, 2, 3, List.of(493, 2930, 39))
        );

        List<Integer> flatList = tree.stream()
                .flatMap(Main::flatten)
                .collect(Collectors.toList());

        System.out.println(flatList);
    }

    private static Stream<Integer> flatten(List<?> list) {
        return list.stream()
                .flatMap(o -> o instanceof List ? flatten((List<?>) o) : Stream.of((Integer) o));
    }
}