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;
- import java.util.stream.Collectors;
- public class _10_PopulationCounter {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Country=> City, Population
- Map<String, Map<String, Long>> countries = new LinkedHashMap<>();
- String input = "";
- while (!"report".equals(input = sc.nextLine())) {
- String[] data = input.split("\\|");
- countries.putIfAbsent(data[1], new LinkedHashMap<>());
- Map<String, Long> cities = countries.get(data[1]);
- cities.putIfAbsent(data[0], 0L);
- cities.put(data[0], cities.get(data[0]) + Long.parseLong(data[2]));
- }
- countries
- .entrySet()
- .stream()
- .sorted((p1, p2) -> {
- long pop1 = p1.getValue()
- .values()
- .stream()
- .mapToLong(Long::valueOf)
- .sum();
- long pop2 = p2.getValue()
- .values()
- .stream()
- .mapToLong(Long::valueOf)
- .sum();
- return Long.compare(pop2, pop1);
- })
- .forEach(e -> {
- long pop = e.getValue().values().stream().mapToLong(Long::longValue).sum();
- System.out.println(String.format("%s (total population: %d)", e.getKey(), pop));
- Map<String, Long> sortedCities = e.getValue()
- .entrySet()
- .stream()
- .sorted((c1, c2) -> Long.compare(c2.getValue(), c1.getValue()))
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));
- sortedCities.forEach((key, value) -> System.out.println(String.format("=>%s: %d", key, value)));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement