Advertisement
NinoB

RainAir

Mar 23rd, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7.  
  8. public class New {
  9.     public static void main(String[] args) throws IOException {
  10.  
  11.         BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         Map<String, ArrayList<Integer>> result = new TreeMap<>();
  14.  
  15.         String line;
  16.         while (!"I believe I can fly!".equals(line = console.readLine()))
  17.         {
  18.             if (line.contains("="))
  19.             {
  20.                 String customerName = line.split(" = ")[0];
  21.                 String customer2Name = line.split(" = ")[1];
  22.  
  23.                 ArrayList<Integer> customer2Flights = new ArrayList<>(result.get(customer2Name));
  24.                 if (result.keySet().contains(customerName) && result.keySet().contains(customer2Name))
  25.                 {
  26.                     result.replace(customerName, customer2Flights);
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 String[] data = line.split(" ");
  32.                 String name = data[0];
  33.  
  34.                 for (int i = 1; i < data.length; i++)
  35.                 {
  36.                     result.putIfAbsent(name, new ArrayList<>());
  37.                     if (!result.get(name).contains(Integer.parseInt(data[i])))
  38.                     {
  39.                         result.get(name).add(Integer.parseInt(data[i]));
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.  
  45.         result.entrySet().stream().sorted((x, y) ->
  46.                 Integer.compare(y.getValue().size(), x.getValue().size())
  47.         ).forEachOrdered(z -> {
  48.             z.getValue().sort(Comparator.naturalOrder());
  49.             String flights = z.getValue().stream().map(i -> i.toString()).collect(Collectors.joining(", "));
  50.             System.out.printf("#%s ::: %s%n", z.getKey(), flights);
  51.         });
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement