svetlozar_kirkov

Weightlifting [Java Exam]

Sep 27th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package com.sve;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         Map<String, Map<String, Long>> list = new TreeMap<>();
  10.         long linesCount = Long.parseLong(scan.nextLine());
  11.         for (int i = 0; i < linesCount; i++) {
  12.             String[] tempLineSplit = scan.nextLine().split(" ");
  13.             if (list.containsKey(tempLineSplit[0])) {
  14.                 if (list.get(tempLineSplit[0]).containsKey(tempLineSplit[1])) {
  15.                     Map<String, Long> oldMap = list.get(tempLineSplit[0]);
  16.                     oldMap.put(tempLineSplit[1], oldMap.get(tempLineSplit[1]) + Long.parseLong(tempLineSplit[2]));
  17.                 }
  18.                 else {
  19.                     list.get(tempLineSplit[0]).put(tempLineSplit[1], Long.parseLong(tempLineSplit[2]));
  20.                 }
  21.             }
  22.             else {
  23.                 Map<String,Long> newMap = new TreeMap<>();
  24.                 newMap.put(tempLineSplit[1], Long.parseLong(tempLineSplit[2]));
  25.                 list.put(tempLineSplit[0], newMap);
  26.             }
  27.         }
  28.         for (Map.Entry<String, Map<String, Long>> entry : list.entrySet()) {
  29.             String mapString = entry.getValue().toString()
  30.                     .replace("{", "")
  31.                     .replace("}", "")
  32.                     .replace("=", " - ")
  33.                     .replace(", ", " kg, ")
  34.                     + " kg";
  35.  
  36.             System.out.println(entry.getKey() + " : " + mapString);
  37.         }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment