RGAlex

O8. Company Users / Maps, Lambda and Stream API - Exercise

Mar 10th, 2021
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package MapsLambdaStreamAPI.Exercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class O8CompanyUsers {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String command = scanner.nextLine();
  9.         Map<String, List<String>> companyUsers = new TreeMap<>();
  10.  
  11.         while (!command.equals("End")) {
  12.             String[] tokens = command.split("\\s+->\\s+");
  13.             String company = tokens[0];
  14.             String users = tokens[1];
  15.             if (!companyUsers.containsKey(company)) {
  16.                 companyUsers.put(company, new ArrayList<>());
  17.             }
  18.             if (!companyUsers.get(company).contains(users)) {
  19.                 companyUsers.get(company).add(users);
  20.             }
  21.             command = scanner.nextLine();
  22.         }
  23.         companyUsers.forEach((key, value) -> {
  24.             System.out.println(key);
  25.             value.forEach(users -> System.out.println("-- " + users));
  26.         });
  27.         //for (String s : companyUsers.keySet()) {   //  <- и този начин работи
  28.         // System.out.printf("%s\n", s);
  29.         //  for (int i = 0; i < companyUsers.get(s).size(); i++) {
  30.         //    System.out.printf("-- %s\n", companyUsers.get(s).get(i));
  31.         //  }
  32.         // }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment