fork(1) 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. .map(n -> n.getChildNodes().stream().findFirst().orElse(null))
  49. // The next statement is equivalent to '.filter(Objects::nonNull)'
  50. .filter(n -> n != null)
  51. .collect(Collectors.toList());
  52. list.forEach(System.out::println);
  53. }
  54.  
  55. public static void main(String[] args) {
  56. fixMe();
  57. }
  58. }
  59.  
Success #stdin #stdout 0.11s 36580KB
stdin
Standard input is empty
stdout
name: raw
name: mp4
name: wav