Advertisement
Guest User

Pr04PopulationCounter

a guest
Nov 14th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package ACSharpExam19July2015;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Pr04PopulationCounter {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         LinkedHashMap<String, LinkedHashMap<String, Long>> countrys = new LinkedHashMap<>();
  11.         LinkedHashMap<String, Long> countrysTotal = new LinkedHashMap<>();
  12.         String line = sc.nextLine();
  13.         while (!line.equals("report")) {
  14.             String[] lineParts = line.split("\\|");
  15.             String city = lineParts[0];
  16.             String country = lineParts[1];
  17.             long population = Long.parseLong(lineParts[2]);
  18.             if (!countrys.containsKey(country)) {
  19.                 countrys.put(country, new LinkedHashMap<>());
  20.                 countrysTotal.put(country, 0L);
  21.             }
  22.             countrys.get(country).put(city, population);
  23.             long toAdd = countrysTotal.get(country) + population;
  24.             countrysTotal.put(country, toAdd);
  25.             line = sc.nextLine();
  26.         }
  27.  
  28.         countrysTotal.entrySet().stream()
  29.                 .sorted(Map.Entry.comparingByValue((v1, v2) -> v2.compareTo(v1))).forEach(pair -> {
  30.             System.out.println(String.format("%s (total population: %d)", pair.getKey(), pair.getValue()));
  31.             countrys.get(pair.getKey()).entrySet().stream()
  32.                     .sorted(Map.Entry.comparingByValue((v1, v2) -> v2.compareTo(v1))).forEach(innerPair -> {
  33.                 System.out.println(String.format("=>%s: %d", innerPair.getKey(), innerPair.getValue()));
  34.             });
  35.         });
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement