LardaX

PopulationCounter

Jan 25th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class PopulationCounter {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         LinkedHashMap<String, HashMap<String, Long>> countries = new LinkedHashMap<>();
  7.  
  8.         while (true) {
  9.             String[] input = scanner.nextLine().split("\\|");
  10.  
  11.             if ("report".equals(input[0])) {
  12.                 break;
  13.             }
  14.  
  15.             String city = input[0];
  16.             String country = input[1];
  17.             Long population = Long.parseLong(input[2]);
  18.  
  19.             insertCountry(countries, country);
  20.             insertCity(countries, country, city, population);
  21.         }
  22.  
  23.         Map<String, Long> countriesByPopulation = new LinkedHashMap<>();
  24.  
  25.         for (String country : countries.keySet()) {
  26.             long totalPopulation = 0;
  27.             for (Long populationCount : countries.get(country).values()) {
  28.                 totalPopulation += populationCount;
  29.             }
  30.             countriesByPopulation.put(country, totalPopulation);
  31.         }
  32.  
  33.         Map<String, Long> countriesSorted = new LinkedHashMap<>();
  34.  
  35.         countriesByPopulation.entrySet().stream()
  36.                 .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
  37.                 .forEachOrdered(x -> countriesSorted.put(x.getKey(), x.getValue()));
  38.  
  39.         for (String country : countriesSorted.keySet()) {
  40.             System.out.println(country + " (total population: " + countriesSorted.get(country) + ")");
  41.             LinkedHashMap<String, Long> cities = new LinkedHashMap<>();
  42.  
  43.             countries.get(country).entrySet().stream()
  44.                     .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
  45.                     .forEachOrdered(x -> cities.put(x.getKey(), x.getValue()));
  46.  
  47.             for (String city : cities.keySet()) {
  48.                 System.out.println("=>" + city + ": " + cities.get(city));
  49.             }
  50.         }
  51.  
  52.     }
  53.  
  54.     private static void insertCity(LinkedHashMap<String, HashMap<String, Long>> countries, String country, String city, Long population) {
  55.         if (!countries.get(country).containsKey(city)) {
  56.             countries.get(country).put(city, population);
  57.         }
  58.     }
  59.  
  60.     private static void insertCountry(LinkedHashMap<String, HashMap<String, Long>> countries, String country) {
  61.         if (!countries.containsKey(country)) {
  62.             countries.put(country, new LinkedHashMap<>());
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment