Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class UserMailMerge {
  4.     public static void main(String[] args) {
  5.         List<String> inArr = List.of(
  6.                 "user1->[email protected],[email protected]",
  7.                 "user2->[email protected],[email protected]",
  8.                 "user3->[email protected],[email protected],[email protected]",
  9.                 "user4->[email protected],[email protected],[email protected]",
  10.                 "user5->[email protected]");
  11.         List<String> outArr = new ArrayList();
  12.  
  13.         Map<String, Set<String>> userMailsAsIsMap = new LinkedHashMap<>(); // <user, Set<mail>>
  14.         Map<String, String> mailOfUserMap = new LinkedHashMap<>(); // <mail, user>
  15.  
  16.         for (String rec : inArr){
  17.             String[] userAndMails = rec.split("->");
  18.             String user = userAndMails[0];
  19.             String[] mails = userAndMails[1].split(",");
  20.  
  21.             userMailsAsIsMap.put(user, Set.of(mails));
  22.  
  23.             Set<String> previousUsers = new HashSet();
  24.             Set<String> mailsForAddOrChange = new HashSet();
  25.             for(String mail : mails) {
  26.                 mailsForAddOrChange.add(mail);
  27.                 if(mailOfUserMap.containsKey(mail)){
  28.                     previousUsers.add(mailOfUserMap.get(mail));
  29.                 }
  30.             }
  31.             for(String previousUser : previousUsers){
  32.                 for(String mailForChange : userMailsAsIsMap.get(previousUser)){
  33.                     mailsForAddOrChange.add(mailForChange);
  34.                 }
  35.             }
  36.             if (!previousUsers.isEmpty()){
  37.                 user = previousUsers.iterator().next();
  38.             }
  39.             for (String mailForAddOrChange : mailsForAddOrChange){
  40.                 mailOfUserMap.put(mailForAddOrChange, user);
  41.             }
  42.         }
  43.  
  44.         // fill return resulr
  45.         Map<String, Set<String>> userMailMap = new LinkedHashMap<>(); // <user, Set<mail>>
  46.         for(Map.Entry<String, String> mailUserEntry : mailOfUserMap.entrySet()){
  47.             if (userMailMap.get(mailUserEntry.getValue()) == null){
  48.                 userMailMap.put(mailUserEntry.getValue(), new LinkedHashSet<>());
  49.             }
  50.             userMailMap.get(mailUserEntry.getValue()).add(mailUserEntry.getKey());
  51.         }
  52.         for(Map.Entry<String, Set<String>> mailUserEntry : userMailMap.entrySet()){
  53.             StringBuilder sb = new StringBuilder(mailUserEntry.getKey());
  54.             sb.append("->");
  55.             sb.append(String.join(",", mailUserEntry.getValue()));
  56.             outArr.add(sb.toString());
  57.         }
  58.  
  59.         System.out.println("--- inArr: " + inArr);
  60.         inArr.forEach(System.out::println);
  61.  
  62.         System.out.println("--- outArr: " + outArr);
  63.         outArr.forEach(System.out::println);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement