Advertisement
Guest User

Untitled

a guest
May 25th, 2016
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3.  
  4. public class c1 {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         String input = "";
  8.         LinkedHashMap<String , LinkedHashMap<String, Long>> countriesAndCities = new LinkedHashMap<>();
  9.         LinkedHashMap<String, Long> countriesOnly = new LinkedHashMap<>();
  10.         while(!(input = sc.nextLine()).equals("report")){
  11.             String [] data = input.split("\\|");
  12.             String city = data[0];
  13.             String country = data[1];
  14.             Long population = Long.parseLong(data[2]);
  15.  
  16.             if(!countriesAndCities.containsKey(country)){
  17.                 countriesAndCities.put(country,new LinkedHashMap<>());
  18.                 countriesOnly.put(country, 0L);
  19.             }
  20.             countriesOnly.put(country,countriesOnly.get(country)+population);
  21.             if(!countriesAndCities.get(country).containsKey(city)){
  22.                 countriesAndCities.get(country).put(city,population);
  23.             }
  24.         }
  25.         countriesAndCities.entrySet().stream()
  26.                 .sorted((c1,c2)-> countriesOnly.get(c2.getKey()).compareTo(countriesOnly.get(c1.getKey())))
  27.                 .forEach(country ->{
  28.                     System.out.format("%s (total population: %d)\n",country.getKey(),countriesOnly.get(country.getKey()));
  29.                     country.getValue().entrySet()
  30.                             .stream().sorted((t1,t2)-> t2.getValue().compareTo(t1.getValue())).forEach(city ->{
  31.                             System.out.format("=>%s: %d\n", city.getKey(),city.getValue());
  32.                     });
  33.         });
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement