SHARE
TWEET

Untitled

a guest Nov 12th, 2015 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Created by Lili on 10.11.2015 �..
  8.  */
  9. public class Population_Counter {
  10.     public static void main(String[] args) {
  11.         Scanner scn = new Scanner(System.in);
  12.         Map<String, Long> totalSum = new LinkedHashMap<>();
  13.         LinkedHashMap<String, LinkedHashMap<String, Long>> map = new LinkedHashMap<>();
  14.         String input = scn.nextLine();
  15.         while (!input.equals("report")) {
  16.             String[] arg = input.split("\\|");
  17.             String city = arg[0];
  18.             String country = arg[1];
  19.             long population = Long.parseLong(arg[2]);
  20.             if (!map.containsKey(country)) {
  21.                 map.put(country, new LinkedHashMap<>());
  22.             }
  23.             if (!totalSum.containsKey(country)) {
  24.                 totalSum.put(country, 0L);
  25.             }
  26.             if (!map.get(country).containsKey(city)) {
  27.                 map.get(country).put(city, population);
  28.             }
  29.             totalSum.put(country, totalSum.get(country) + population);
  30.             input = scn.nextLine();
  31.         }
  32.  
  33.         totalSum.entrySet()
  34.                 .stream()
  35.                 .sorted((a,b) -> b.getValue().compareTo(a.getValue()))
  36.                 .forEach(entry -> {
  37.  
  38.                     System.out.printf("%s(total population: %d)\n",
  39.                             entry.getKey(),
  40.                             entry.getValue()
  41.                     );
  42.                     map.get(entry.getKey()).entrySet()
  43.                             .stream()
  44.                             .sorted((a,b)-> b.getValue().compareTo(a.getValue()) )
  45.                             .forEach(m -> System.out.printf("=>%s: %d\n",
  46.                                     m.getKey(),
  47.                                     m.getValue()));
  48.                 });
  49.  
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top