svetlozar_kirkov

Logs Aggregator

Feb 5th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.Set;
  5. import java.util.TreeMap;
  6. import java.util.TreeSet;
  7.  
  8. public class LogsAggregator {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner input = new Scanner(System.in);
  12.         int n = input.nextInt();
  13.         input.nextLine();
  14.         Map<String,String> nameAndIP = new TreeMap<>();
  15.         Map<String,Integer> nameAndDuration = new TreeMap<>();
  16.         for (int i = 0; i < n; i++){
  17.             String[] tempInput = input.nextLine().split("[\\s]+");
  18.             if (nameAndIP.containsKey(tempInput[1])){
  19.                 String oldIPs = nameAndIP.get(tempInput[1]);
  20.                 nameAndIP.put(tempInput[1], oldIPs+tempInput[0]+" ");
  21.             }
  22.             else{
  23.                 nameAndIP.put(tempInput[1],tempInput[0]+" ");
  24.             }
  25.             if (nameAndDuration.containsKey(tempInput[1])){
  26.                 int oldCount = nameAndDuration.get(tempInput[1]);
  27.                 nameAndDuration.put(tempInput[1], oldCount+Integer.parseInt(tempInput[2]));
  28.             }
  29.             else{
  30.                 nameAndDuration.put(tempInput[1], Integer.parseInt(tempInput[2]));
  31.             }
  32.         }
  33.        
  34.         for(Map.Entry<String,String> entry : nameAndIP.entrySet()) {
  35.             String name = entry.getKey();
  36.             String[] ipListTemp = entry.getValue().split("[\\s]+");
  37.             Set<String> ipListSet = new TreeSet<>(Arrays.asList(ipListTemp));
  38.             String[] ipListCleaned = ipListSet.toArray(new String[ipListSet.size()]);
  39.             int duration = nameAndDuration.get(name);
  40.             System.out.printf("%s: %d [", name,duration);
  41.             for (int j = 0; j < ipListCleaned.length; j++){
  42.                 if (j == ipListCleaned.length-1){
  43.                     System.out.printf("%s]\n", ipListCleaned[j]);
  44.                 }
  45.                 else{
  46.                     System.out.printf("%s, ", ipListCleaned[j]);
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment