svetlozar_kirkov

User Logs

Mar 15th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. package javabasics;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8. import java.util.Set;
  9. import java.util.TreeMap;
  10.  
  11. public class UserLogs {
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner input = new Scanner(System.in);
  15.         String current = input.nextLine();
  16.         Map<String,LinkedHashMap<String,Integer>> info = new TreeMap<>();
  17.         while (!current.equals("end")){
  18.             String[] userInfo = current.split(" ");
  19.             String address = userInfo[0].substring(3);
  20.             String user = userInfo[2].substring(5);
  21.             if (info.containsKey(user)){
  22.                 LinkedHashMap<String,Integer> old = info.get(user);
  23.                 if (old.containsKey(address)){
  24.                     int oldCount = old.get(address);
  25.                     old.put(address, oldCount+1);
  26.                     info.put(user, old);
  27.                 }
  28.                 else {
  29.                     old.put(address, 1);
  30.                     info.put(user, old);
  31.                 }
  32.             }
  33.             else {
  34.                 LinkedHashMap<String,Integer> newInfo = new LinkedHashMap<>();
  35.                 newInfo.put(address, 1);
  36.                 info.put(user, newInfo);
  37.             }
  38.             current = input.nextLine();
  39.         }
  40.         Set<String> keys = info.keySet();
  41.         for(String key: keys){
  42.             System.out.println(key+":");
  43.             HashMap values = info.get(key);
  44.             Set<String> keysVal = values.keySet();
  45.             Collection valuesVal = values.values();
  46.             int count = 0;
  47.             for(String k: keysVal){
  48.                 System.out.print(k + " => " + values.get(k));
  49.                 count++;
  50.                 if (count == values.size()){
  51.                     System.out.println(".");
  52.                 }
  53.                 else {
  54.                     System.out.print(", ");
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment