import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public final class Main {
public static final class Node {
private final List<Node> children;
}
this(name,
Arrays.
stream(children
) .map(Node::new)
.collect(Collectors.toUnmodifiableList()));
}
private Node
(String name, List
<Node
> list
) { this.name = name;
this.children = list;
}
public List<Node> getChildNodes() {
return children;
}
@Override
return "name: " + name;
}
}
private static List<Node> getNodes() {
new Node("image", "raw", "jpeg", "png"),
new Node("video", "mp4"),
new Node("game"),
new Node("music", "wav", "mp3"));
}
private static void fixMe() {
var list = getNodes().stream()
.map(n -> n.getChildNodes().stream().findFirst().orElse(null))
// The next statement is equivalent to '.filter(Objects::nonNull)'
.filter(n -> n != null)
.collect(Collectors.toList());
list.
forEach(System.
out::println
); }
public static void main
(String[] args
) { fixMe();
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CmltcG9ydCBqYXZhLnV0aWwuQ29sbGVjdGlvbnM7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5zdHJlYW0uQ29sbGVjdG9yczsKCnB1YmxpYyBmaW5hbCBjbGFzcyBNYWluIHsKCiAgICBwdWJsaWMgc3RhdGljIGZpbmFsIGNsYXNzIE5vZGUgewoKICAgICAgICBwcml2YXRlIGZpbmFsIFN0cmluZyBuYW1lOwogICAgICAgIHByaXZhdGUgZmluYWwgTGlzdDxOb2RlPiBjaGlsZHJlbjsKCiAgICAgICAgcHVibGljIE5vZGUoU3RyaW5nIG5hbWUpIHsKICAgICAgICAgICAgdGhpcyhuYW1lLCBDb2xsZWN0aW9ucy5lbXB0eUxpc3QoKSk7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgTm9kZShTdHJpbmcgbmFtZSwgU3RyaW5nLi4uIGNoaWxkcmVuKSB7CiAgICAgICAgICAgIHRoaXMobmFtZSwgQXJyYXlzLnN0cmVhbShjaGlsZHJlbikKICAgICAgICAgICAgICAgIC5tYXAoTm9kZTo6bmV3KQogICAgICAgICAg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bCknCiAgICAgICAgICAgIC5maWx0ZXIobiAtPiBuICE9IG51bGwpCiAgICAgICAgICAgIC5jb2xsZWN0KENvbGxlY3RvcnMudG9MaXN0KCkpOwogICAgICAgIGxpc3QuZm9yRWFjaChTeXN0ZW0ub3V0OjpwcmludGxuKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgZml4TWUoKTsKICAgIH0KfQo=