Advertisement
nikeza

Problem 10.* Population Counter

Oct 2nd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class map1_Exarcises_10Population_Counter {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine();
  7.  
  8.         Map<String, Map<String, Long>> countres = new LinkedHashMap<>();
  9.         while (!input.equals("report")) {
  10.             String[] tokens = input.split("\\|");
  11.             String name = tokens[1];
  12.             String city = tokens[0];
  13.             long population = Long.parseLong(tokens[2]);
  14.             countres.putIfAbsent(name, new LinkedHashMap<>());
  15.             countres.get(name).put(city, population);
  16.  
  17.             input = scanner.nextLine();
  18.         }
  19.         countres.entrySet().stream()
  20.                 .sorted((f, s) -> s.getValue().values().stream().reduce(0L, Long::sum)
  21.                         .compareTo(f.getValue().values().stream().reduce(0L, Long::sum))
  22.                 ).forEach(entry -> {
  23.             //  long totalPopulation = population(entry.getValue());
  24.             System.out.printf("%s (total population: %d)%n", entry.getKey(), entry.getValue().values()
  25.                     .stream().reduce(0L, Long::sum));
  26.             entry.getValue().entrySet().stream()
  27.                     .sorted((f, s) -> s.getValue().compareTo(f.getValue()))
  28.                     .forEach(e -> {
  29.                         System.out.printf("=>%s: %d%n", e.getKey(), e.getValue());
  30.                     });
  31.         });
  32.  
  33.      
  34.     }
  35.  
  36.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement