Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class _10_PopulationCounter {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Country => City, Population
- Map<String, Map<String, Integer>> population = new LinkedHashMap<>();
- String str = "";
- while (!"report".equals(str = sc.nextLine())) {
- String[] data = str.split("\\|");
- String country = data[1];
- String city = data[0];
- int p = Integer.parseInt(data[2]);
- population.putIfAbsent(country, new LinkedHashMap<>());
- population.get(country).putIfAbsent(city, 0);
- population.get(country).put(city, population.get(country).get(city) + p);
- }
- population.entrySet().stream().sorted((e1, e2) -> {
- long first = e1.getValue()
- .values()
- .stream()
- .mapToLong(Long::valueOf)
- .sum();
- long second = e2.getValue()
- .values()
- .stream()
- .mapToLong(Long::valueOf)
- .sum();
- return Long.compare(second, first);
- }).forEach(p -> {
- long sum = p.getValue().values().stream().mapToLong(Long::valueOf).sum();
- System.out.println(String.format("%s (total population: %d)",
- p.getKey(), sum));
- p.getValue().entrySet().stream().sorted((pop1, pop2) -> {
- return Long.compare(pop2.getValue(), pop1.getValue());
- }).forEach((kvp) -> {
- System.out.println(String.format("=>%s: %d", kvp.getKey(), kvp.getValue()));
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement