Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- /**
- * Created by Lili on 10.11.2015 �..
- */
- public class Population_Counter {
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- Map<String, Long> totalSum = new LinkedHashMap<>();
- LinkedHashMap<String, LinkedHashMap<String, Long>> map = new LinkedHashMap<>();
- String input = scn.nextLine();
- while (!input.equals("report")) {
- String[] arg = input.split("\\|");
- String city = arg[0];
- String country = arg[1];
- long population = Long.parseLong(arg[2]);
- if (!map.containsKey(country)) {
- map.put(country, new LinkedHashMap<>());
- }
- if (!totalSum.containsKey(country)) {
- totalSum.put(country, 0L);
- }
- if (!map.get(country).containsKey(city)) {
- map.get(country).put(city, population);
- }
- totalSum.put(country, totalSum.get(country) + population);
- input = scn.nextLine();
- }
- totalSum.entrySet()
- .stream()
- .sorted((a,b) -> b.getValue().compareTo(a.getValue()))
- .forEach(entry -> {
- System.out.printf("%s(total population: %d)\n",
- entry.getKey(),
- entry.getValue()
- );
- map.get(entry.getKey()).entrySet()
- .stream()
- .sorted((a,b)-> b.getValue().compareTo(a.getValue()) )
- .forEach(m -> System.out.printf("=>%s: %d\n",
- m.getKey(),
- m.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement