Didart

Company Users

Nov 7th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CompanyUsers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.  
  9.         LinkedHashMap<String, List<String>> companyName = new LinkedHashMap<>();
  10.  
  11.         while (!input.equals("End")) {
  12.             String data[] = input.split(" -> ");
  13.             if (companyName.containsKey(data[0])) {
  14.                 List<String> employees = companyName.get(data[0]);
  15.                 if (!employees.contains(data[1])) {
  16.                     employees.add(data[1]);
  17.                 }
  18.             } else {
  19.                 List<String> employees = new ArrayList<>();
  20.                 employees.add(data[1]);
  21.                 companyName.put(data[0], employees);
  22.             }
  23.             input = scanner.nextLine();
  24.         }
  25.         companyName.entrySet()
  26.                 .stream()
  27.                 .sorted(Map.Entry.comparingByKey())
  28.                 .forEach(company -> { System.out.println(company.getKey());
  29.                     company.getValue()
  30.                             .forEach(employee -> System.out.println("-- " + employee));
  31.                 });
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment