Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 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. import java.util.Map;
  6.  
  7. public class Pr14PopulationCounter {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         Map<String, Map<String, Long>> data = new LinkedHashMap<>();
  12.         String line = reader.readLine();
  13.  
  14.         while (!"report".equals(line)) {
  15.             String[] tokens = line.split("\\|");
  16.             String city = tokens[0];
  17.             String country = tokens[1];
  18.             long population = Long.parseLong(tokens[2]);
  19.  
  20.             if (!data.containsKey(country)) {
  21.                 data.put(country, new LinkedHashMap<>());
  22.             }
  23.             data.get(country).put(city, population);
  24.  
  25.             line = reader.readLine();
  26.         }
  27.  
  28.         data.entrySet().stream()
  29.                 .sorted((country1, country2) -> country2.getValue().values().stream().reduce(0L, Long::sum)
  30.                         .compareTo(country1.getValue().values().stream().reduce(0L, Long::sum)))
  31.                 .forEach(country -> {
  32.                     System.out.printf("%s (total population: %d)%n", country.getKey(),
  33.                             country.getValue().values().stream().reduce(0L, Long::sum));
  34.                     country.getValue().entrySet().stream()
  35.                             .sorted((city1, city2) -> city2.getValue().compareTo(city1.getValue()))
  36.                             .forEach(city -> System.out.printf("=>%s: %d%n", city.getKey(), city.getValue()));
  37.                 });
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement