Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 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, Long>> countryCity = new LinkedHashMap<>();
  10. while (!"report".equals(input[0])) {
  11. String city = input[0];
  12. String country = input[1];
  13. long people = Long.parseLong(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. long sum1 = e1.getValue().values().stream().mapToLong(d->d).sum();
  22. long sum2 = e2.getValue().values().stream().mapToLong(d->d ).sum();
  23. if (sum1 == sum2) {
  24. return e1.getKey().compareTo(e2.getKey());
  25. } else {
  26. return (int) (sum2-sum1);
  27. }
  28. }).forEach(key -> {
  29. long sum = key.getValue().values().stream().mapToLong(d->d).sum();
  30. System.out.printf("%s (total population: %d)%n", key.getKey(), sum);
  31. key.getValue().entrySet().stream().sorted((a, b) -> {
  32. if(a.getValue().equals(b.getValue())){
  33. return a.getValue().compareTo(b.getValue());
  34. }else{
  35. return b.getValue().compareTo(a.getValue());
  36. }
  37. })
  38. .forEach(e ->
  39. System.out.printf("=>%s: %d%n", e.getKey(), e.getValue()));
  40. });
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement