Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sve;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, Map<String, Long>> list = new TreeMap<>();
- long linesCount = Long.parseLong(scan.nextLine());
- for (int i = 0; i < linesCount; i++) {
- String[] tempLineSplit = scan.nextLine().split(" ");
- if (list.containsKey(tempLineSplit[0])) {
- if (list.get(tempLineSplit[0]).containsKey(tempLineSplit[1])) {
- Map<String, Long> oldMap = list.get(tempLineSplit[0]);
- oldMap.put(tempLineSplit[1], oldMap.get(tempLineSplit[1]) + Long.parseLong(tempLineSplit[2]));
- }
- else {
- list.get(tempLineSplit[0]).put(tempLineSplit[1], Long.parseLong(tempLineSplit[2]));
- }
- }
- else {
- Map<String,Long> newMap = new TreeMap<>();
- newMap.put(tempLineSplit[1], Long.parseLong(tempLineSplit[2]));
- list.put(tempLineSplit[0], newMap);
- }
- }
- for (Map.Entry<String, Map<String, Long>> entry : list.entrySet()) {
- String mapString = entry.getValue().toString()
- .replace("{", "")
- .replace("}", "")
- .replace("=", " - ")
- .replace(", ", " kg, ")
- + " kg";
- System.out.println(entry.getKey() + " : " + mapString);
- }
- }
- }
Add Comment
Please, Sign In to add comment