veronikaaa86

Population Counter 11

Feb 20th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 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 Demo {
  7.     public static void main(String[] args) throws IOException { //Need trows this Exception
  8.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //Instead Scanner
  9.  
  10.         LinkedHashMap<String,LinkedHashMap<String,Integer>> citiesPopulation = new LinkedHashMap<>();
  11.         LinkedHashMap<String,Long> countryPopulation = new LinkedHashMap<>();
  12.  
  13.         String[] rawInput = reader.readLine().split("\\|"); // Read from console...After write reader.readLine() ->
  14.                                                             //Alt + Enter -> for trow Exception
  15.  
  16.         while(!rawInput[0].equals("report")){
  17.  
  18.             String country = rawInput[1];
  19.             String city = rawInput[0];
  20.             Integer population = Integer.parseInt(rawInput[2]);
  21.  
  22.             citiesPopulation.putIfAbsent(country, new LinkedHashMap<>());
  23.             citiesPopulation.get(country).put(city,population);
  24.  
  25.             countryPopulation.putIfAbsent(country,0L);
  26.             countryPopulation.put(country,countryPopulation.get(country)+population);
  27.  
  28.             rawInput = reader.readLine().split("\\|");
  29.         }
  30.  
  31.         countryPopulation.entrySet().stream().sorted((a,b)->b.getValue().compareTo(a.getValue()))
  32.                 .forEach(c-> {
  33.                     System.out.printf("%s (total population: %d)%n", c.getKey(), c.getValue());
  34.                     citiesPopulation.get(c.getKey()).entrySet().stream()
  35.                             .sorted((a,b)->b.getValue().compareTo(a.getValue()))
  36.                             .forEach(town-> System.out.printf("=>%s: %d%n", town.getKey(), town.getValue()));
  37.  
  38.                 });
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment