RGAlex

09. User Logs / Sets And Maps Advanced - Exercises

May 31st, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class UserLogs {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         Map<String, Map<String, Integer>> usersInfo = new TreeMap<>();
  9.         String input;
  10.         while (!"end".equals(input = scan.nextLine())) {
  11.             String[] inputData = input.split("\\s+");
  12.             String userName = inputData[2].split("=")[1];
  13.             String ipAddress = inputData[0].split("=")[1];
  14.             usersInfo.putIfAbsent(userName, new LinkedHashMap<>());
  15.             usersInfo.get(userName).put(ipAddress, !usersInfo.get(userName).containsKey(ipAddress)
  16.                     ? 1 : usersInfo.get(userName).get(ipAddress) + 1);
  17.         }
  18.         usersInfo.forEach((key, value) -> {
  19.             System.out.printf("%s:%n", key);
  20.             int count = 1;
  21.             for (Map.Entry<String, Integer> innerMap : value.entrySet()) {
  22.                 String output = String.format(count < value.entrySet().size()
  23.                         ? "%s => %d, " : "%s => %d.%n", innerMap.getKey(), innerMap.getValue());
  24.                 System.out.printf("%s", output);
  25.                 count++;
  26.             }
  27.         });
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment