Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Pr14PopulationCounter {
- 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 = reader.readLine();
- while (!"report".equals(line)) {
- 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);
- line = reader.readLine();
- }
- data.entrySet().stream()
- .sorted((country1, country2) -> country2.getValue().values().stream().reduce(0L, Long::sum)
- .compareTo(country1.getValue().values().stream().reduce(0L, Long::sum)))
- .forEach(country -> {
- System.out.printf("%s (total population: %d)%n", country.getKey(),
- country.getValue().values().stream().reduce(0L, Long::sum));
- country.getValue().entrySet().stream()
- .sorted((city1, city2) -> city2.getValue().compareTo(city1.getValue()))
- .forEach(city -> System.out.printf("=>%s: %d%n", city.getKey(), city.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement