flattern Tree

flat a tree
 avatar
user_9487184
java
2 years ago
622 B
4
Indexable
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));
    }
}
Editor is loading...