fork download
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5.  
  6. public final class Main {
  7.  
  8. public static final class Node {
  9.  
  10. private final String name;
  11. private final List<Node> children;
  12.  
  13. public Node(String name) {
  14. this(name, Collections.emptyList());
  15. }
  16.  
  17. public Node(String name, String... children) {
  18. this(name, Arrays.stream(children)
  19. .map(Node::new)
  20. .collect(Collectors.toUnmodifiableList()));
  21. }
  22.  
  23. private Node(String name, List<Node> list) {
  24. this.name = name;
  25. this.children = list;
  26. }
  27.  
  28. public List<Node> getChildNodes() {
  29. return children;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. return "name: " + name;
  35. }
  36. }
  37.  
  38. private static List<Node> getNodes() {
  39. return List.of(
  40. new Node("image", "raw", "jpeg", "png"),
  41. new Node("video", "mp4"),
  42. new Node("game"),
  43. new Node("music", "wav", "mp3"));
  44. }
  45.  
  46. private static void fixMe() {
  47. var list = getNodes().stream()
  48. .flatMap(n -> n.getChildNodes().stream().findFirst().stream())
  49. .collect(Collectors.toList());
  50. list.forEach(System.out::println);
  51. }
  52.  
  53. public static void main(String[] args) {
  54. fixMe();
  55. }
  56. }
  57.  
Success #stdin #stdout 0.13s 36528KB
stdin
Standard input is empty
stdout
name: raw
name: mp4
name: wav