-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserLogs.java
More file actions
73 lines (55 loc) · 2.12 KB
/
UserLogs.java
File metadata and controls
73 lines (55 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package UserLogs;
import jdk.nashorn.internal.runtime.regexp.joni.ScanEnvironment;
import java.util.*;
public class UserLogs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Map<String,Integer>> usersAndIp = new TreeMap<>();
while(true){
String[] input = scanner.nextLine().split("\\s+");
if(input[0].equals("end"))
break;
String ip = equalSplit(input[0]);
String username = equalSplit(input[2]);
if(!usersAndIp.containsKey(username)){
Map<String,Integer> map = new LinkedHashMap<>();
map.put(ip,1);
usersAndIp.put(username,map);
}
else{
Map<String,Integer> map = usersAndIp.get(username);
if(map.containsKey(ip)){
int ipCount = map.get(ip);
map.put(ip,ipCount + 1);
}
else{
map.put(ip,1);
}
}
}
for (Map.Entry<String, Map<String, Integer>> stringMapEntry : usersAndIp.entrySet()) {
System.out.printf("%s:%n",stringMapEntry.getKey());
if(stringMapEntry.getValue().size() > 1){
int counter = stringMapEntry.getValue().size() - 1;
for (Map.Entry<String, Integer> m : stringMapEntry.getValue().entrySet()) {
if(counter == 0){
System.out.printf("%s => %d.%n", m.getKey(), m.getValue());
}
else{
System.out.printf("%s => %d, ", m.getKey(), m.getValue());
}
counter--;
}
}
else {
for (Map.Entry<String, Integer> m : stringMapEntry.getValue().entrySet()) {
System.out.printf("%s => %d.%n", m.getKey(), m.getValue());
}
}
}
}
public static String equalSplit(String input){
String[] str = input.split("=");
return str[1];
}
}