Advertisement
dimipan80

Exam 4. Logs Aggregator

Sep 15th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map.Entry;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5. import java.util.TreeSet;
  6.  
  7. public class _4_LogsAggregator {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         int count = scan.nextInt();
  13.         scan.nextLine();
  14.  
  15.         TreeMap<String, Integer> durations = new TreeMap<>();
  16.         HashMap<String, TreeSet<String>> ipAddresses = new HashMap<>();
  17.  
  18.         for (int i = 0; i < count; i++) {
  19.             String ipStr = scan.next();
  20.             String userName = scan.next();
  21.             String durStr = scan.next();
  22.             int duration = Integer.parseInt(durStr);
  23.  
  24.             Integer previousDuration = durations.get(userName);
  25.             if (previousDuration == null) {
  26.                 previousDuration = 0;
  27.             }
  28.             durations.put(userName, previousDuration + duration);
  29.  
  30.             TreeSet<String> ipSet = ipAddresses.get(userName);
  31.             if (ipSet == null) {
  32.                 ipSet = new TreeSet<>();
  33.             }
  34.             ipSet.add(ipStr);
  35.             ipAddresses.put(userName, ipSet);
  36.  
  37.             scan.nextLine();
  38.         }
  39.  
  40.         for (Entry<String, Integer> userNameAndDuration : durations.entrySet()) {
  41.             String userName = userNameAndDuration.getKey();
  42.             int duration = userNameAndDuration.getValue();
  43.             TreeSet<String> ipSet = ipAddresses.get(userName);
  44.             System.out.println(userName + ": " + duration + " " + ipSet);
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement