desislava_topuzakova

Untitled

Nov 13th, 2021
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Snowwhite {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String input;
  10.  
  11. Map<String, Integer> dwarfs = new LinkedHashMap<>();
  12.  
  13. while (!(input = scanner.nextLine()).equals("Once upon a time")) {
  14. String[] tokens = input.split("<:>");
  15. String dwarfName = tokens[0].trim();
  16. String hatColor = tokens[1].trim();
  17. int physics = Integer.parseInt(tokens[2].trim());
  18.  
  19. String key = dwarfName + ":" + hatColor;
  20.  
  21. dwarfs.putIfAbsent(key, physics);
  22. if (!dwarfs.containsKey(key)) {
  23. dwarfs.put(key, physics);
  24. } else {
  25. if (dwarfs.get(key) < physics) {
  26. dwarfs.put(key, physics);
  27. }
  28. }
  29. }
  30.  
  31. dwarfs.entrySet()
  32. .stream()
  33. .sorted((e1, e2) -> {
  34. int res = e2.getValue().compareTo(e1.getValue());
  35. if (res == 0) {
  36. String x = e1.getKey().split(":")[1];
  37. String y = e2.getKey().split(":")[1];
  38. res = Long.compare(dwarfs.keySet().stream().filter(v -> v.split(":")[1].equals(y)).count(),
  39. dwarfs.keySet().stream().filter(v -> v.split(":")[1].equals(x)).count());
  40. }
  41. return res;
  42. }).forEach(e -> {
  43. System.out.printf("(%s) %s <-> %d%n",
  44. e.getKey().split(":")[1],
  45. e.getKey().split(":")[0],
  46. e.getValue());
  47. });
  48. }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment