Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.TreeMap;
- public class LogsAggregator {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- input.nextLine();
- TreeMap<String, TreeMap<String, Integer>> allUsers = new TreeMap<>();
- // fill map with ips,names,duration of all inputLines
- for (int i = 0; i < n; i++) {
- String[] inputLine = input.nextLine().split(" ");
- String ip = inputLine[0];
- String name = inputLine[1];
- int duration = Integer.parseInt(inputLine[2]);
- if (!allUsers.containsKey(name)) {
- allUsers.put(name, new TreeMap<>());
- allUsers.get(name).put(ip, duration);
- } else if (allUsers.get(name).containsKey(ip)) {
- int currentDuration = allUsers.get(name).get(ip);
- allUsers.get(name).put(ip, currentDuration + duration);
- } else {
- allUsers.get(name).put(ip, duration);
- }
- }
- // print user-duration-ips
- for (String user : allUsers.keySet()) {
- System.out.print(user + ": ");
- int allMinutes = 0;
- for (String ip : allUsers.get(user).keySet()) {
- allMinutes += allUsers.get(user).get(ip);
- }
- System.out.print(allMinutes + " ");
- System.out.print(allUsers.get(user).keySet());
- System.out.println();
- }
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment