Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class map1_Exarcises_10Population_Counter {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, Map<String, Long>> countres = new LinkedHashMap<>();
- while (!input.equals("report")) {
- String[] tokens = input.split("\\|");
- String name = tokens[1];
- String city = tokens[0];
- long population = Long.parseLong(tokens[2]);
- countres.putIfAbsent(name, new LinkedHashMap<>());
- countres.get(name).put(city, population);
- input = scanner.nextLine();
- }
- countres.entrySet().stream()
- .sorted((f, s) -> s.getValue().values().stream().reduce(0L, Long::sum)
- .compareTo(f.getValue().values().stream().reduce(0L, Long::sum))
- ).forEach(entry -> {
- // long totalPopulation = population(entry.getValue());
- System.out.printf("%s (total population: %d)%n", entry.getKey(), entry.getValue().values()
- .stream().reduce(0L, Long::sum));
- entry.getValue().entrySet().stream()
- .sorted((f, s) -> s.getValue().compareTo(f.getValue()))
- .forEach(e -> {
- System.out.printf("=>%s: %d%n", e.getKey(), e.getValue());
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement