fork(1) download
  1. import java.util.List;
  2. import java.util.Map;
  3. import java.util.stream.Collectors;
  4. import static java.util.Map.entry;
  5.  
  6. public final class Main {
  7.  
  8. private static final Map<String, Runnable> COMMAND_MAP = Map.ofEntries(
  9. entry("help", () -> printHelp()),
  10. entry("quit", () -> exitProgram()),
  11. entry("shutdown", () -> shutdownSystem()));
  12.  
  13. private static void printHelp() {
  14. System.out.println("printHelp");
  15. }
  16.  
  17. private static void exitProgram() {
  18. System.out.println("exitProgram");
  19. }
  20.  
  21. private static void shutdownSystem() {
  22. System.out.println("shutdownSystem");
  23. }
  24.  
  25. private static List<String> getCommandList() {
  26. return List.of("help", "reboot", "shutdown");
  27. }
  28.  
  29. private static void fixMe() {
  30. var list = getCommandList().stream()
  31. // Defers disposition of 'null' when 'Map#get(Object)' returns
  32. // 'null'
  33. .map(COMMAND_MAP::get)
  34. // The next statement is equivalent to '.filter(Objects::nonNull)'
  35. .filter(r -> r != null)
  36. .collect(Collectors.toList());
  37. list.forEach(r -> r.run());
  38. }
  39.  
  40. public static void main(String[] args) {
  41. fixMe();
  42. }
  43. }
  44.  
Success #stdin #stdout 0.08s 34180KB
stdin
Standard input is empty
stdout
printHelp
shutdownSystem