Advertisement
N_Damyanov

02. Excel Functions

Feb 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int line = Integer.parseInt(scanner.nextLine());
  9.         List<String> header = Arrays.stream(scanner.nextLine().split(", +"))
  10.                 .collect(Collectors.toCollection(ArrayList::new));
  11.  
  12.         List<List<String>> table = new ArrayList<>();
  13.  
  14.  
  15.         for (int row = 0; row < line - 1; row++) {
  16.             List<String> input = Arrays.stream(scanner.nextLine().split(", +"))
  17.                     .collect(Collectors.toCollection(ArrayList::new));
  18.             table.add(input);
  19.         }
  20.  
  21.         String[] command = scanner.nextLine().split("\\s+");
  22.         int colIndex = header.indexOf(command[1]);
  23.  
  24.         List<List<String>> result = new ArrayList<>();
  25.  
  26.         switch (command[0]) {
  27.             case "sort":
  28.                 result = table.stream()
  29.                         .sorted(Comparator.comparing(a -> a.get(colIndex))).collect(Collectors.toList());
  30.                 break;
  31.             case "hide":
  32.                 table.forEach(e -> e.remove(colIndex));
  33.                 header.remove(colIndex);
  34.                 result = table;
  35.                 break;
  36.             case "filter":
  37.                 String toFilter = command[2];
  38.                 result = table.stream().filter(e -> e.get(colIndex).equals(toFilter)).collect(Collectors.toList());
  39.                 break;
  40.         }
  41.         System.out.println(String.join(" | ", header));
  42.         result.forEach(e -> System.out.println(String.join(" | ", e)));
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement