Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.stream.Collectors;
- public class Population {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, Map<String, Long>> data = new LinkedHashMap<>();
- String line;
- while (!"report".equals((line = reader.readLine()))) {
- String[] tokens = line.split("\\|");
- String city = tokens[0];
- String country = tokens[1];
- Long population = Long.parseLong(tokens[2]);
- if (!data.containsKey(country)) {
- data.put(country, new LinkedHashMap<>());
- }
- data.get(country).put(city, population);
- }
- data.entrySet().stream().sorted((c1, c2) -> Long.compare(c2.getValue().values().stream().collect(Collectors.summarizingLong(Long::valueOf)).getSum()
- , c1.getValue().values().stream().collect(Collectors.summarizingLong(Long::valueOf)).getSum())).forEach(country -> {
- System.out.println(String.format("%s (total population: %d)", country.getKey(),
- country.getValue().values().stream().collect(Collectors.summarizingLong(Long::valueOf)).getSum()));
- country.getValue().entrySet().stream().sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue()))
- .forEach(pair -> System.out.println(String.format("=>%s: %d", pair.getKey(), pair.getValue())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement