John_IV

Untitled

Jan 26th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. package Testing;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.math.BigInteger;
  7. import java.text.DecimalFormat;
  8. import java.time.LocalDate;
  9. import java.time.LocalTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.*;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collectors;
  15.  
  16. public class _test2 {
  17. public static void main(String[] args) {
  18. Scanner scanner = new Scanner(System.in);
  19. String[] input = scanner.nextLine().split("\\|");
  20. Map<String, Map<String, Long>> countryCity = new LinkedHashMap<>();
  21.  
  22. while (!"report".equals(input[0])) {
  23. String city = input[0];
  24. String country = input[1];
  25. long people = Long.parseLong(input[2]);
  26. countryCity.putIfAbsent(country, new LinkedHashMap<>());
  27. countryCity.get(country).put(city, people);
  28. input = scanner.nextLine().split("\\|");
  29. }
  30. countryCity.entrySet()
  31. .stream()
  32. .sorted((e1, e2) -> {
  33. long sum1 = e1.getValue().values().stream().mapToLong(d -> d).sum();
  34. long sum2 = e2.getValue().values().stream().mapToLong(d -> d).sum();
  35. return Long.compare(sum2, sum1);
  36.  
  37. }).forEach(key -> {
  38.  
  39. long sum = key.getValue().values().stream().mapToLong(d -> d).sum();
  40. System.out.printf("%s (total population: %d)%n", key.getKey(), sum);
  41.  
  42. key.getValue().entrySet().stream()
  43. .sorted((a, b) -> b.getValue().compareTo(a.getValue()))
  44. .forEach(e ->
  45. System.out.printf("=>%s: %d%n", e.getKey(), e.getValue()));
  46. });
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment