import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.
util.
Map.
entry;
public final class Main {
private static final Map
<String, Runnable
> COMMAND_MAP
= Map.
ofEntries( entry("help", () -> printHelp()),
entry("quit", () -> exitProgram()),
entry("shutdown", () -> shutdownSystem()));
private static void printHelp() {
System.
out.
println("printHelp"); }
private static void exitProgram() {
System.
out.
println("exitProgram"); }
private static void shutdownSystem() {
System.
out.
println("shutdownSystem"); }
private static List<String> getCommandList() {
return List.
of("help",
"reboot",
"shutdown"); }
private static void fixMe() {
var list = getCommandList().stream()
.flatMap(n -> Optional.ofNullable(COMMAND_MAP.get(n)).stream())
.collect(Collectors.toList());
// The next is also fine, but the number of elements differs:
//
// var list = getCommandList().stream()
// .map(n -> COMMAND_MAP.getOrDefault(n, () -> {}))
// .collect(Collectors.toList());
list.forEach(r -> r.run());
}
public static void main
(String[] args
) { fixMe();
}
}
aW1wb3J0IGphdmEudXRpbC5MaXN0OwppbXBvcnQgamF2YS51dGlsLk1hcDsKaW1wb3J0IGphdmEudXRpbC5PcHRpb25hbDsKaW1wb3J0IGphdmEudXRpbC5zdHJlYW0uQ29sbGVjdG9yczsKaW1wb3J0IHN0YXRpYyBqYXZhLnV0aWwuTWFwLmVudHJ5OwoKcHVibGljIGZpbmFsIGNsYXNzIE1haW4gewoKICAgIHByaXZhdGUgc3RhdGljIGZpbmFsIE1hcDxTdHJpbmcsIFJ1bm5hYmxlPiBDT01NQU5EX01BUCA9IE1hcC5vZkVudHJpZXMoCiAgICAgICAgICAgIGVudHJ5KCJoZWxwIiwgKCkgLT4gcHJpbnRIZWxwKCkpLAogICAgICAgICAgICBlbnRyeSgicXVpdCIsICgpIC0+IGV4aXRQcm9ncmFtKCkpLAogICAgICAgICAgICBlbnRyeSgic2h1dGRvd24iLCAoKSAtPiBzaHV0ZG93blN5c3RlbSgpKSk7CgogICAgcHJpdmF0ZSBzdGF0aWMgdm9pZCBwcmludEhlbHAoKSB7CiAgICAgICAgU3lz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