Advertisement
Guest User

Untitled

a guest
Dec 29th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader; // 100/100
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. public class _14_Population_Counter {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  9.         Map<String, LinkedHashMap<String, Integer>> countryAndCityPopulation = new LinkedHashMap<>();
  10.         Map<String, Long> countryPopulation = new LinkedHashMap<>();
  11.  
  12.         enterCountriesAndPopulation(bf, countryAndCityPopulation, countryPopulation);
  13.  
  14.         LinkedHashMap<String, Long> orderedByCountryPopulation = countryPopulation.entrySet().stream()
  15.                 .sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
  16.                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  17.  
  18.         for (Map.Entry<String, Long> country : orderedByCountryPopulation.entrySet()) {
  19.             System.out.printf("%s (total population: %d)%n", country.getKey(), country.getValue());
  20.  
  21.             LinkedHashMap<String, Integer> orderedCitiesByPopulation = countryAndCityPopulation.get(country.getKey())
  22.                     .entrySet().stream()
  23.                     .sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
  24.                     .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  25.  
  26.             for (Map.Entry<String, Integer> city : orderedCitiesByPopulation.entrySet()) {
  27.                 System.out.printf("=>%s: %d%n", city.getKey(), city.getValue());
  28.             }
  29.         }
  30.     }
  31.  
  32.     private static void enterCountriesAndPopulation(BufferedReader bf,
  33.                                                     Map<String, LinkedHashMap<String, Integer>> countryAndCityPopulation,
  34.                                                     Map<String, Long> countryPopulation) throws IOException {
  35.         while (true) {
  36.             String[] line = bf.readLine().split("\\|");
  37.  
  38.             if ("report".equalsIgnoreCase(line[0])) {
  39.                 break;
  40.             }
  41.  
  42.             String city = line[0];
  43.             String country = line[1];
  44.             int population = Integer.valueOf(line[2]);
  45.  
  46.             if (!countryAndCityPopulation.containsKey(country)) {
  47.                 LinkedHashMap<String, Integer> cityPopulation = new LinkedHashMap<>();
  48.  
  49.                 countryAndCityPopulation.put(country, cityPopulation);
  50.                 countryPopulation.put(country, 0L);
  51.             }
  52.  
  53.             LinkedHashMap<String, Integer> citiesPopulation = countryAndCityPopulation.get(country);
  54.             citiesPopulation.put(city, population);
  55.             countryAndCityPopulation.put(country, citiesPopulation);
  56.             long totalCountryPopulation = countryPopulation.get(country) + population;
  57.             countryPopulation.put(country, totalCountryPopulation);
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement