Didart

Population Counter

Jan 20th, 2023
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package SetsAndMaps3;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Scanner;
  5. import java.util.concurrent.atomic.AtomicReference;
  6.  
  7. public class PopulationCounter {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = scanner.nextLine();
  12.  
  13.         LinkedHashMap<String, LinkedHashMap<String, Long>> populationByCountries = new LinkedHashMap<>();
  14.  
  15.         while (!input.equals("report")) {
  16.  
  17.             String[] tokens = input.split("\\|");
  18.  
  19.             String city = tokens[0];
  20.             String country = tokens[1];
  21.             Long population = Long.parseLong(tokens[2]);
  22.  
  23.             if (!populationByCountries.containsKey(country)) {
  24.                 populationByCountries.put(country, new LinkedHashMap<>() {{
  25.                     put(city, population);
  26.                 }});
  27.             } else {
  28.                 populationByCountries.get(country).put(city, population);
  29.             }
  30.             input = scanner.nextLine();
  31.         }
  32.  
  33.         populationByCountries
  34.                 .entrySet()
  35.                 .stream()
  36.                 .sorted((e1, e2) -> {
  37.             long totalPopulationFirst = populationByCountries
  38.                     .get(e1.getKey())
  39.                     .values()
  40.                     .stream()
  41.                     .mapToLong(l -> l)
  42.                     .sum();
  43.  
  44.             long totalPopulationSecond = populationByCountries
  45.                     .get(e2.getKey())
  46.                     .values()
  47.                     .stream()
  48.                     .mapToLong(l -> l)
  49.                     .sum();
  50.  
  51.             return Long.compare(totalPopulationSecond, totalPopulationFirst);
  52.         }).forEach(entry -> {
  53.             System.out.print(entry.getKey() + " ");
  54.  
  55.             StringBuilder builder = new StringBuilder();
  56.  
  57.             AtomicReference<Long> totalPopulation = new AtomicReference<>((long) 0);
  58.  
  59.             populationByCountries.get(entry.getKey()).entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
  60.                     .forEach(e -> {
  61.                         builder.append(String.format("=>%s: %d%n", e.getKey(), e.getValue()));
  62.                         totalPopulation.updateAndGet(v -> v + e.getValue());
  63.                     });
  64.  
  65.             System.out.printf("(total population: %s)%n", totalPopulation);
  66.             System.out.print(builder);
  67.         });
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment