Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- import sun.reflect.generics.tree.Tree;
- /**
- * Created by Siderov on 10.11.2015 �..
- */
- public class Population_Counter {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input = sc.nextLine();
- // country city pop
- TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();
- while (!input.equals("report")) {
- String[] inputArgs = input.split("\\|");
- String city = inputArgs[0];
- String country = inputArgs[1];
- Integer pop = Integer.parseInt(inputArgs[2]);
- if (!map.containsKey(country)) {
- map.put(country, new TreeMap<String, Integer>() {{
- put(city, pop);
- put("total", pop);
- }});
- } else if (!map.get(country).containsKey(city)) {
- map.get(country).put(city, pop);
- map.get(country).put("total", map.get(country).get("total")+pop);
- }
- input = sc.nextLine();
- }
- for (String county : map.keySet()) {
- System.out.printf(county + "(total population: %d)", map.get(county).get("total"));
- String line = "";
- for (String city : map.get(county).keySet()) {
- line += String.format(" %s => %d\n", city, map.get(county).get(city));
- }
- System.out.println(line.substring(0, line.length()));
- }
- /*TreeMap<String,TreeMap<String,Integer>> newMap = map.entrySet()
- .stream()
- .sorted((e1, e2) -> e1.getValue().get("total").compareTo(e2.getValue().get("total"))) // compare totals
- .map(e -> e.getKey()).collect(Collectors.toMap())*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement