Advertisement
cankocanko

ForceBook

Sep 5th, 2020
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7. static String newSide = "";
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Scanner sc = new Scanner(System.in);
  12.  
  13. HashMap<String, List<String>> map = new HashMap<>();
  14.  
  15.  
  16. String command = sc.nextLine();
  17.  
  18.  
  19. while (!"Lumpawaroo".equals(command)) {
  20.  
  21.  
  22. ArrayList<String> list = new ArrayList<>();
  23.  
  24. if (command.contains("|")) {
  25.  
  26. String[] commandParts = command.split("\\s+");
  27.  
  28. if (!map.containsKey(commandParts[0])) {
  29. list.add(commandParts[2]);
  30. map.put(commandParts[0], list);
  31. } else {
  32. map.get(commandParts[0]).add(commandParts[2]);
  33. map.put(commandParts[0], map.get(commandParts[0])); // try without this one
  34. }
  35. }
  36. if (command.contains("->")) {
  37.  
  38. String[] commandParts = command.split(" -> ");
  39.  
  40. map.entrySet().stream().filter(e -> !e.getKey().equals(commandParts[1]))
  41. .forEach(stringListEntry -> newSide = stringListEntry.getKey());
  42.  
  43. if (map.get(commandParts[1]).contains(commandParts[0])) {
  44. map.get(commandParts[1]).remove(commandParts[0]);
  45.  
  46. map.get(newSide).add(commandParts[0]);
  47. System.out.println(String.format("%s joins the %s side!", commandParts[0], newSide));
  48.  
  49. } else {
  50. if (map.get(newSide).contains(commandParts[0])) {
  51. map.get(newSide).remove(commandParts[0]);
  52.  
  53. map.get(commandParts[1]).add(commandParts[0]);
  54. } else {
  55. map.get(commandParts[1]).add(commandParts[0]);
  56. map.put(commandParts[1], map.get(commandParts[1]));
  57. }
  58. System.out.println(String.format("%s joins the %s side!", commandParts[0], commandParts[1]));
  59.  
  60. }
  61. }
  62. command = sc.nextLine();
  63. }
  64.  
  65. map.entrySet().stream().filter(e -> e.getValue().size() > 0)
  66. .sorted((n1, n2) -> Integer.compare(n2.getValue().size(), n1.getValue().size()))
  67. .forEach(stringListEntry -> {
  68. System.out.println(String.format("Side: %s, Members: %d",
  69. stringListEntry.getKey(), stringListEntry.getValue().size()));
  70. Collections.sort(stringListEntry.getValue());
  71. for (String s :
  72. stringListEntry.getValue()) {
  73. System.out.println(String.format("! %s",s));
  74. }
  75.  
  76. });
  77.  
  78.  
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement