Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- public class New {
- public static void main(String[] args) throws IOException {
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
- Map<String, ArrayList<Integer>> result = new TreeMap<>();
- String line;
- while (!"I believe I can fly!".equals(line = console.readLine()))
- {
- if (line.contains("="))
- {
- String customerName = line.split(" = ")[0];
- String customer2Name = line.split(" = ")[1];
- ArrayList<Integer> customer2Flights = new ArrayList<>(result.get(customer2Name));
- if (result.keySet().contains(customerName) && result.keySet().contains(customer2Name))
- {
- result.replace(customerName, customer2Flights);
- }
- }
- else
- {
- String[] data = line.split(" ");
- String name = data[0];
- for (int i = 1; i < data.length; i++)
- {
- result.putIfAbsent(name, new ArrayList<>());
- if (!result.get(name).contains(Integer.parseInt(data[i])))
- {
- result.get(name).add(Integer.parseInt(data[i]));
- }
- }
- }
- }
- result.entrySet().stream().sorted((x, y) ->
- Integer.compare(y.getValue().size(), x.getValue().size())
- ).forEachOrdered(z -> {
- z.getValue().sort(Comparator.naturalOrder());
- String flights = z.getValue().stream().map(i -> i.toString()).collect(Collectors.joining(", "));
- System.out.printf("#%s ::: %s%n", z.getKey(), flights);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement