Advertisement
damesova

Population Counter [Mimi][JA]

May 23rd, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class _10_PopulationCounter {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         //  Country =>  City,   Population
  10.         Map<String, Map<String, Integer>> population = new LinkedHashMap<>();
  11.  
  12.         String str = "";
  13.         while (!"report".equals(str = sc.nextLine())) {
  14.  
  15.             String[] data = str.split("\\|");
  16.             String country = data[1];
  17.             String city = data[0];
  18.             int p = Integer.parseInt(data[2]);
  19.  
  20.             population.putIfAbsent(country, new LinkedHashMap<>());
  21.             population.get(country).putIfAbsent(city, 0);
  22.             population.get(country).put(city, population.get(country).get(city) + p);
  23.  
  24.         }
  25.  
  26.         population.entrySet().stream().sorted((e1, e2) -> {
  27.             long first = e1.getValue()
  28.                     .values()
  29.                     .stream()
  30.                     .mapToLong(Long::valueOf)
  31.                     .sum();
  32.             long second = e2.getValue()
  33.                     .values()
  34.                     .stream()
  35.                     .mapToLong(Long::valueOf)
  36.                     .sum();
  37.             return Long.compare(second, first);
  38.         }).forEach(p -> {
  39.             long sum = p.getValue().values().stream().mapToLong(Long::valueOf).sum();
  40.  
  41.             System.out.println(String.format("%s (total population: %d)",
  42.                     p.getKey(), sum));
  43.  
  44.             p.getValue().entrySet().stream().sorted((pop1, pop2) -> {
  45.                 return Long.compare(pop2.getValue(), pop1.getValue());
  46.  
  47.             }).forEach((kvp) -> {
  48.                 System.out.println(String.format("=>%s: %d", kvp.getKey(), kvp.getValue()));
  49.             });
  50.         });
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement