Advertisement
Edzhevit

CompanyUsers

Nov 4th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package AssociativeArraysExercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class CompanyUsers {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         String line = reader.readLine();
  12.         Map<String, List<String>> companyNames = new TreeMap<>();
  13.  
  14.         while (!line.equals("end")){
  15.             String[] input = line.split(" -> ");
  16.             String company = input[0];
  17.             String name = input[1];
  18.  
  19.             if (!companyNames.containsKey(company)){
  20.                 companyNames.put(company, new ArrayList<>());
  21.                 if (!companyNames.get(company).contains(name)){
  22.                     companyNames.get(company).add(name);
  23.                 }
  24.  
  25.             }
  26.  
  27.  
  28.             line = reader.readLine();
  29.         }
  30.  
  31.         companyNames.entrySet().stream().sorted((e1, e2) -> {
  32.             int x1 = e1.getValue().size();
  33.             int x2 = e2.getValue().size();
  34.             return Integer.compare(x1, x2);
  35.         }).forEach(e -> {
  36.             System.out.println(e.getKey());
  37.             Collections.sort(e.getValue());
  38.             for (String names : e.getValue()) {
  39.                 System.out.println("-- " + names);
  40.             }
  41.         });
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement