Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package SetsAndMaps;
  2.  
  3. import java.util.*;
  4.  
  5. public class PopulationCounter {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String[] input = scanner.nextLine().split("\\|");
  9. Map<String, Map<String, Integer>> countryCity = new LinkedHashMap<>();
  10. while (!"report".equals(input[0])) {
  11. String city = input[0];
  12. String country = input[1];
  13. int people = Integer.parseInt(input[2]);
  14. countryCity.putIfAbsent(country, new LinkedHashMap<>());
  15. countryCity.get(country).put(city, people);
  16. input = scanner.nextLine().split("\\|");
  17. }
  18. countryCity.entrySet()
  19. .stream()
  20. .sorted((e1, e2) -> {
  21. int sum1 = e1.getValue().values().stream().mapToInt(Integer::intValue).sum();
  22. int sum2 = e2.getValue().values().stream().mapToInt(Integer::intValue).sum();
  23. if (sum1 == sum2) {
  24. return e1.getKey().compareTo(e2.getKey());
  25. } else {
  26. return sum2 - sum1;
  27. }
  28. }).forEach(key -> {
  29. long sum = key.getValue().values().stream().mapToLong(Integer::longValue).sum();
  30. System.out.printf("%s (total population: %d)%n", key.getKey(), sum);
  31. key.getValue().entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
  32. .forEach(e -> {
  33. System.out.printf("=>%s: %d%n", e.getKey(), e.getValue());
  34. });
  35. });
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement