Advertisement
Guest User

Untitled

a guest
Oct 19th, 2021
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package Advanced.FunctionalProgramming;
  2. //80/100
  3. import java.util.*;
  4. import java.util.function.Predicate;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String input = scanner.nextLine();
  11.         if(input.length()==0){
  12.             return;
  13.         }
  14.         List<String> names = Arrays.stream(input.split("\\s+")).collect(Collectors.toList());
  15.         String line = scanner.nextLine();
  16.         List<String> commandLines =  new ArrayList<>();
  17.  
  18.         while (!line.equals("Print")) {
  19.             String[] lineParts = line.split(";");
  20.             String commands = lineParts[0];
  21.             String type = lineParts[1];
  22.             String toCheck = lineParts[2];
  23.  
  24.             if (commands.equals("Add filter")) {
  25.                 commandLines.add(line);
  26.             } else if (commands.equals("Remove filter")) {
  27.                 String commandToRemove = "Add filter;" + type + ";" + toCheck;
  28.                 commandLines.removeIf(s -> s.equals(commandToRemove));
  29.             }
  30.             line = scanner.nextLine();
  31.         }
  32.         for (String command : commandLines) {
  33.             String[] lineParts = command.split(";");
  34.             String type = lineParts[1];
  35.             String toCheck = lineParts[2];
  36.  
  37.             Predicate<String> startsWith = name -> name.startsWith(toCheck);
  38.             Predicate<String> endsWith = name -> name.endsWith(toCheck);
  39.             Predicate<String> validLenght = name -> name.length() == Integer.parseInt(toCheck);
  40.             Predicate<String> containsLetter = name -> name.contains(toCheck);
  41.  
  42.             switch (type) {
  43.                 case "Starts with":
  44.                     names.removeIf(startsWith);
  45.                     break;
  46.                 case "Ends with":
  47.                     names.removeIf(endsWith);
  48.                     break;
  49.                 case "Length":
  50.                     names.removeIf(validLenght);
  51.                     break;
  52.                 case "Contains":
  53.                     names.removeIf(containsLetter);
  54.                     break;
  55.             }
  56.         }
  57.         System.out.println(names.stream().collect(Collectors.joining(" ")).toString().replaceAll("([\\[\\]])", ""));
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement