borovaneca

Count Population

Jan 30th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.LinkedHashMap;
  5.  
  6. public class PopulationCounter {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10.         LinkedHashMap<String, LinkedHashMap<String, Long>> countries = new LinkedHashMap<>();
  11.  
  12.         String line;
  13.         while (!(line = reader.readLine()).equals("report")) {
  14.             String[] tokens = line.split("\\|");
  15.  
  16.             String town = tokens[0];
  17.             String country = tokens[1];
  18.             long population = Long.parseLong(tokens[2]);
  19.  
  20.             if (!countries.containsKey(country)) {
  21.                 countries.put(country, new LinkedHashMap<>()); //add the country if absent
  22.             }
  23.  
  24.             if (!countries.get(country).containsKey(town)) {
  25.                 countries.get(country).put(town, population); //add the city if absent
  26.             } else {
  27.                 countries.get(country).put(town, countries.get(country).get(town) + population); //increment the city population in the given country
  28.             }
  29.         }
  30.  
  31.         countries.entrySet()
  32.                 .stream()
  33.                 .sorted((c1, c2) -> Long.compare(c2.getValue() //Countries should be ordered by their total population in descending order, so we sum all cities population
  34.                                 .values()
  35.                                 .stream()
  36.                                 .mapToLong(Long::longValue)
  37.                                 .sum(),
  38.                         c1.getValue()
  39.                                 .values()
  40.                                 .stream()
  41.                                 .mapToLong(Long::longValue)
  42.                                 .sum()))
  43.                 .forEach(c -> {
  44.                     System.out.printf("%s (total population: %d)%n",
  45.                             c.getKey(),
  46.                             c.getValue()
  47.                                     .values()
  48.                                     .stream()
  49.                                     .mapToLong(Long::longValue)
  50.                                     .sum());
  51.  
  52.                     c.getValue()
  53.                             .entrySet()
  54.                             .stream()
  55.                             .sorted((t1, t2) -> Long.compare(t2.getValue(), t1.getValue())) //the cities should be ordered by the same criterion
  56.                             .forEach(t -> System.out.printf("=>%s: %d%n", t.getKey(), t.getValue()));
  57.                 });
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment