Advertisement
Guest User

Untitled

a guest
Nov 12th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4. import java.util.stream.Collectors;
  5. import sun.reflect.generics.tree.Tree;
  6.  
  7. /**
  8. * Created by Siderov on 10.11.2015 �..
  9. */
  10. public class Population_Counter {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. String input = sc.nextLine();
  14. // country city pop
  15. TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();
  16. while (!input.equals("report")) {
  17.  
  18. String[] inputArgs = input.split("\\|");
  19.  
  20. String city = inputArgs[0];
  21. String country = inputArgs[1];
  22. Integer pop = Integer.parseInt(inputArgs[2]);
  23.  
  24. if (!map.containsKey(country)) {
  25. map.put(country, new TreeMap<String, Integer>() {{
  26. put(city, pop);
  27. put("total", pop);
  28. }});
  29. } else if (!map.get(country).containsKey(city)) {
  30. map.get(country).put(city, pop);
  31. map.get(country).put("total", map.get(country).get("total")+pop);
  32. }
  33.  
  34. input = sc.nextLine();
  35. }
  36. for (String county : map.keySet()) {
  37. System.out.printf(county + "(total population: %d)", map.get(county).get("total"));
  38. String line = "";
  39. for (String city : map.get(county).keySet()) {
  40. line += String.format(" %s => %d\n", city, map.get(county).get(city));
  41. }
  42. System.out.println(line.substring(0, line.length()));
  43. }
  44.  
  45. /*TreeMap<String,TreeMap<String,Integer>> newMap = map.entrySet()
  46. .stream()
  47. .sorted((e1, e2) -> e1.getValue().get("total").compareTo(e2.getValue().get("total"))) // compare totals
  48. .map(e -> e.getKey()).collect(Collectors.toMap())*/
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement