Created
September 21, 2014 08:36
-
-
Save boyanov83/5207758daa4c965f0830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment