Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SetsAndMaps;
- import java.util.*;
- public class PopulationCounter {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split("\\|");
- Map<String, Map<String, Integer>> countryCity = new LinkedHashMap<>();
- while (!"report".equals(input[0])) {
- String city = input[0];
- String country = input[1];
- int people = Integer.parseInt(input[2]);
- countryCity.putIfAbsent(country, new LinkedHashMap<>());
- countryCity.get(country).put(city, people);
- input = scanner.nextLine().split("\\|");
- }
- countryCity.entrySet()
- .stream()
- .sorted((e1, e2) -> {
- int sum1 = e1.getValue().values().stream().mapToInt(Integer::intValue).sum();
- int sum2 = e2.getValue().values().stream().mapToInt(Integer::intValue).sum();
- if (sum1 == sum2) {
- return e1.getKey().compareTo(e2.getKey());
- } else {
- return sum2 - sum1;
- }
- }).forEach(key -> {
- long sum = key.getValue().values().stream().mapToLong(Integer::longValue).sum();
- System.out.printf("%s (total population: %d)%n", key.getKey(), sum);
- key.getValue().entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
- .forEach(e -> {
- System.out.printf("=>%s: %d%n", e.getKey(), e.getValue());
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement