Advertisement
vlastomar

Untitled

Oct 3rd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class UseraLogs {
  7. public static void main(String[] args) {
  8. Scanner scan = new Scanner(System.in);
  9.  
  10. Map<String, LinkedHashMap<String, Integer>> ip = new TreeMap<>();
  11.  
  12. String input = scan.nextLine();
  13. while (!"end".equals(input)){
  14. String[] token = input.split("\\s+");
  15. String[] ipAdressArr = token[0].split("=");
  16. String ipAddress = ipAdressArr[1];
  17. String[] name = token[token.length - 1].split("=");
  18. String username = name[1];
  19.  
  20. ip.putIfAbsent(username, new LinkedHashMap<>());
  21. /*if (ip.get(username).containsKey(ipAddress)){
  22. ip.get(username).put(ipAddress, ip.get(username).get(ipAddress) + 1);
  23. }else {
  24. ip.get(username).put(ipAddress, 1);
  25. }*/
  26. ip.get(username).putIfAbsent(ipAddress, 0);
  27. ip.get(username).put(ipAddress, ip.get(username).get(ipAddress) + 1);
  28.  
  29. input = scan.nextLine();
  30. }
  31. for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : ip.entrySet()) {
  32. System.out.println(entry.getKey() + ":" + " ");
  33. int[] counter = {0};
  34. entry.getValue().entrySet()
  35. .forEach(p ->{
  36. if (counter[0] < entry.getValue().size() - 1){
  37. System.out.print(String.format("%s => %d, ", p.getKey(), p.getValue()));
  38. }else{
  39. System.out.println(String.format("%s => %d.", p.getKey(), p.getValue()));
  40. }
  41. counter[0]++;
  42. });
  43. }
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement