LoraOrliGeo

P8_Company Users_Maps_Ex

Apr 15th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package ex1_MapsLambdaStreamAPI_13March2019;
  2.  
  3. import java.util.*;
  4.  
  5. public class P8_CompanyUsers {
  6.     public static void main(String[] args) {
  7.         @SuppressWarnings("resource")
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         Map<String, List<String>> companyUser = new TreeMap<>();
  12.  
  13.         String line = "";
  14.  
  15.         while (!"End".equals(line = sc.nextLine())) {
  16.             String input = line;
  17.             String[] data = input.split(" -> ");
  18.             String company = data[0];
  19.             String userID = data[1];
  20.  
  21.             if (!companyUser.containsKey(company)) {
  22.                 companyUser.put(company, new ArrayList<>());
  23.                 companyUser.get(company).add(userID);
  24.             } else {
  25.                 if (!companyUser.get(company).contains(userID)) {
  26.                     companyUser.get(company).add(userID);
  27.                 }
  28.             }
  29.         }
  30.  
  31.         companyUser.entrySet().stream().forEach(kvp -> {
  32.             System.out.println(kvp.getKey());
  33.             for (String id : kvp.getValue()) {
  34.                 System.out.println(String.format("-- %s", id));
  35.             }
  36.         });
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment