Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.TreeSet;
- public class LogsAggregator {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- input.nextLine();
- Map<String,String> nameAndIP = new TreeMap<>();
- Map<String,Integer> nameAndDuration = new TreeMap<>();
- for (int i = 0; i < n; i++){
- String[] tempInput = input.nextLine().split("[\\s]+");
- if (nameAndIP.containsKey(tempInput[1])){
- String oldIPs = nameAndIP.get(tempInput[1]);
- nameAndIP.put(tempInput[1], oldIPs+tempInput[0]+" ");
- }
- else{
- nameAndIP.put(tempInput[1],tempInput[0]+" ");
- }
- if (nameAndDuration.containsKey(tempInput[1])){
- int oldCount = nameAndDuration.get(tempInput[1]);
- nameAndDuration.put(tempInput[1], oldCount+Integer.parseInt(tempInput[2]));
- }
- else{
- nameAndDuration.put(tempInput[1], Integer.parseInt(tempInput[2]));
- }
- }
- for(Map.Entry<String,String> entry : nameAndIP.entrySet()) {
- String name = entry.getKey();
- String[] ipListTemp = entry.getValue().split("[\\s]+");
- Set<String> ipListSet = new TreeSet<>(Arrays.asList(ipListTemp));
- String[] ipListCleaned = ipListSet.toArray(new String[ipListSet.size()]);
- int duration = nameAndDuration.get(name);
- System.out.printf("%s: %d [", name,duration);
- for (int j = 0; j < ipListCleaned.length; j++){
- if (j == ipListCleaned.length-1){
- System.out.printf("%s]\n", ipListCleaned[j]);
- }
- else{
- System.out.printf("%s, ", ipListCleaned[j]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment