Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Main {
- static String newSide = "";
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- HashMap<String, List<String>> map = new HashMap<>();
- String command = sc.nextLine();
- while (!"Lumpawaroo".equals(command)) {
- ArrayList<String> list = new ArrayList<>();
- if (command.contains("|")) {
- String[] commandParts = command.split("\\s+");
- if (!map.containsKey(commandParts[0])) {
- list.add(commandParts[2]);
- map.put(commandParts[0], list);
- } else {
- map.get(commandParts[0]).add(commandParts[2]);
- map.put(commandParts[0], map.get(commandParts[0])); // try without this one
- }
- }
- if (command.contains("->")) {
- String[] commandParts = command.split(" -> ");
- map.entrySet().stream().filter(e -> !e.getKey().equals(commandParts[1]))
- .forEach(stringListEntry -> newSide = stringListEntry.getKey());
- if (map.get(commandParts[1]).contains(commandParts[0])) {
- map.get(commandParts[1]).remove(commandParts[0]);
- map.get(newSide).add(commandParts[0]);
- System.out.println(String.format("%s joins the %s side!", commandParts[0], newSide));
- } else {
- if (map.get(newSide).contains(commandParts[0])) {
- map.get(newSide).remove(commandParts[0]);
- map.get(commandParts[1]).add(commandParts[0]);
- } else {
- map.get(commandParts[1]).add(commandParts[0]);
- map.put(commandParts[1], map.get(commandParts[1]));
- }
- System.out.println(String.format("%s joins the %s side!", commandParts[0], commandParts[1]));
- }
- }
- command = sc.nextLine();
- }
- map.entrySet().stream().filter(e -> e.getValue().size() > 0)
- .sorted((n1, n2) -> Integer.compare(n2.getValue().size(), n1.getValue().size()))
- .forEach(stringListEntry -> {
- System.out.println(String.format("Side: %s, Members: %d",
- stringListEntry.getKey(), stringListEntry.getValue().size()));
- Collections.sort(stringListEntry.getValue());
- for (String s :
- stringListEntry.getValue()) {
- System.out.println(String.format("! %s",s));
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement