Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SetsAndMapAdvanced_Exercise;
- import java.util.*;
- public class UserLogs_09 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //user -> {ip -> count}
- TreeMap<String, LinkedHashMap<String, Integer>> data = new TreeMap<>();
- String input = scanner.nextLine();
- //"IP={IP.Address} message={A&sample&message} user={username}"
- while(!input.equals("end")) {
- String[] inputParts = input.split("\\s+"); //["IP={IP.Address}", "message={A&sample&message}", "user={username}"]
- String ip = inputParts[0].split("=")[1]; //"IP={IP.Address}".split("=") -> ["IP","{ip addres}"]
- //String message = inputParts[1].split("=")[1]; // "message={A&sample&message}".split("=") -> ["message", "{A&sample&message}"]
- String username = inputParts[2].split("=")[1]; //"user={username}".split("=") -> ["user", "{username}"]
- //username да не съществува
- if (!data.containsKey(username)) {
- data.put(username, new LinkedHashMap<>(){
- {
- put(ip, 1);
- }
- });
- }
- // username да съществува
- else {
- //текущия списък с ip
- Map<String, Integer> currentIps = data.get(username); //списък с ip-та (ip -> бр срещанията)
- //ip да е посетено (има в списъка)
- if (currentIps.containsKey(ip)) {
- int currentTimes = currentIps.get(ip);
- currentIps.put(ip, currentTimes + 1);
- }
- //ip не е посещавано (няма в списъка)
- else {
- currentIps.put(ip, 1);
- }
- }
- input = scanner.nextLine();
- }
- //"username:
- //{IP} => {count}, {IP} => {count}."
- data.keySet().forEach(username -> {
- System.out.println(username + ":");
- Map<String, Integer> ips = data.get(username);
- //запис: ip -> бр. срещанията
- ips.entrySet().forEach(entry -> {
- //if не е последното ip
- System.out.println(entry.getKey() + " => " + entry.getValue() + ", ");
- //TODO: последното ip да се отпчатва с точка накрая
- //if е последното ip в map-a
- //System.out.println(entry.getKey() + " => " + entry.getValue() + ". ");
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement