Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import java.util.*;
  4.  
  5. public class ExcelFunctions2 { // solution with Lists
  6.  
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. int n = Integer.parseInt(sc.nextLine());
  11. ArrayList<ArrayList<String>> matrix = new ArrayList<>();
  12.  
  13. while (n-- > 0) {
  14. String[] data = sc.nextLine().split("[,\\s]+");
  15. matrix.addAll(
  16. Collections.singleton(
  17. new ArrayList<>(Arrays.asList(data))));
  18. }
  19.  
  20. String[] line = sc.nextLine().split(" ");
  21.  
  22. switch (line[0]) {
  23. case "sort":
  24. sortMatrix(matrix, line[1]);
  25. break;
  26. case "hide":
  27. hide(matrix, line[1]);
  28. break;
  29. case "filter":
  30. filter(matrix, line[1], line[2]);
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36.  
  37. private static void sortMatrix(ArrayList<ArrayList<String>> matrix, String param) {
  38. int headerIndex = matrix.get(0).indexOf(param);
  39. System.out.println(print(matrix.get(0)));
  40.  
  41. matrix
  42. .stream()
  43. .skip(1)
  44. .sorted(Comparator.comparing(o -> o.get(headerIndex)))
  45. .forEach(rows -> System.out.println(print(rows)));
  46. }
  47.  
  48. private static void filter(ArrayList<ArrayList<String>> matrix, String headerName, String param) {
  49. int headerIndex = matrix.get(0).indexOf(headerName);
  50. System.out.println(print(matrix.get(0)));
  51.  
  52. matrix
  53. .stream()
  54. .skip(1)
  55. .filter(rows -> rows.get(headerIndex).contains(param))
  56. .forEach(row -> System.out.println(print(row)));
  57. }
  58.  
  59. private static void hide(ArrayList<ArrayList<String>> matrix, String param) {
  60. int colToHide = matrix.get(0).indexOf(param);
  61.  
  62. for (ArrayList<String> rows : matrix) {
  63. rows.remove(colToHide);
  64. }
  65.  
  66. for (ArrayList<String> rows : matrix) {
  67. System.out.println(print(rows));
  68. }
  69. }
  70.  
  71. private static String print(ArrayList<String> matrix) {
  72. return String.join(" | ", matrix);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement