Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Testing;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.math.BigInteger;
- import java.text.DecimalFormat;
- import java.time.LocalDate;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class _test2 {
- 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();
- return Long.compare(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) -> 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