Advertisement
Guest User

Untitled

a guest
Nov 7th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Nuts {
  4. public static void main(String[] args) {
  5.  
  6. Scanner scn = new Scanner(System.in);
  7. int n = Integer.parseInt(scn.nextLine());
  8. TreeMap<String, LinkedHashMap<String, Long>> companies = new TreeMap<>();
  9.  
  10. for (int i = 0; i < n; i++) {
  11. String[] line = scn.nextLine().split("\\s+");
  12. String company = line[0];
  13. String nuts = line[1];
  14. Long amount = Long.parseLong(line[2].replaceAll("kg", ""));
  15.  
  16. if (!companies.containsKey(company)) {
  17. companies.put(company, new LinkedHashMap<>());
  18. }
  19. Long count = companies.get(company).get(nuts);
  20. if (count == null) {
  21. count = 0l;
  22. }
  23. companies.get(company).put(nuts, amount + count);
  24. }
  25. StringBuilder sb = new StringBuilder();
  26. for (Map.Entry<String, LinkedHashMap<String, Long>> item : companies.entrySet()) {
  27. sb.setLength(0);
  28. sb.append(item.getKey() + ": ");
  29. item.getValue().entrySet().stream()
  30. .forEach((entry) -> {
  31. sb.append(String.format("%s-%dkg, ", entry.getKey(), entry.getValue()));
  32. });
  33. System.out.println(sb.substring(0, sb.length() - 2));
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement