Advertisement
petur_stoqnov

Java

Mar 12th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CompanyUsers {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         String command = sc.nextLine();
  7.  
  8.         LinkedHashMap<String, ArrayList<String>> hashMap = new LinkedHashMap<>();
  9.  
  10.         while (!command.equalsIgnoreCase("End")) {
  11.             String[] input = command.split(" -> ");
  12.             String name = input[0];
  13.             String id = input[1];
  14.  
  15.             hashMap.putIfAbsent(name, new ArrayList<>());
  16.                 if(!hashMap.get(name).contains(id)){
  17.                     hashMap.get(name).add(id);
  18.                 }
  19.  
  20.  
  21.             command = sc.nextLine();
  22.         }
  23.         hashMap.entrySet()
  24.                 .stream()
  25.                 .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey()))
  26.                 .forEach(e -> {
  27.                     System.out.printf("%s%n", e.getKey());
  28.                     for (int i = 0; i < e.getValue().size(); i++) {
  29.                         System.out.printf("-- %s%n", e.getValue().get(i));
  30.                     }
  31.                 });
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement