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, Long>> countryCity = new LinkedHashMap<>();
- while (!"report".equals(input[0])) {
- String city = input[0];
- String country = input[1];
- long people = Long.parseLong(input[2]);
- countryCity.putIfAbsent(country, new LinkedHashMap<>());
- countryCity.get(country).put(city, people);
- input = scanner.nextLine().split("\\|");
- }
- countryCity.entrySet()
- .stream()
- .sorted((e1, e2) -> {
- long sum1 = e1.getValue().values().stream().mapToLong(d->d).sum();
- long sum2 = e2.getValue().values().stream().mapToLong(d->d ).sum();
- if (sum1 == sum2) {
- return e1.getKey().compareTo(e2.getKey());
- } else {
- return (int) (sum2-sum1);
- }
- }).forEach(key -> {
- long sum = key.getValue().values().stream().mapToLong(d->d).sum();
- System.out.printf("%s (total population: %d)%n", key.getKey(), sum);
- key.getValue().entrySet().stream().sorted((a, b) -> {
- if(a.getValue().equals(b.getValue())){
- return a.getValue().compareTo(b.getValue());
- }else{
- return 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