Advertisement
RGAlex

O1. Ranking / Maps, Lambda and Stream API - More Exercise

Mar 27th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package MapsLambdaStreamAPI.MoreExercises;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7.  
  8. public class O1Ranking {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         LinkedHashMap<String, String> contests = new LinkedHashMap<>();
  12.         TreeMap<String, LinkedHashMap<String, Integer>> users = new TreeMap<>();
  13.         String input = scanner.nextLine();
  14.         while (!input.equals("end of contests")) {
  15.             String[] tokens = input.split(":");
  16.             String contest = tokens[0];
  17.             String password = tokens[1];
  18.             contests.put(contest, password);
  19.             input = scanner.nextLine();
  20.         }
  21.  
  22.         input = scanner.nextLine();
  23.         while (!input.equals("end of submissions")) {
  24.             String[] tokens = input.split("=>");
  25.             String contest = tokens[0];
  26.             String password = tokens[1];
  27.             String userName = tokens[2];
  28.             int userPoints = Integer.parseInt(tokens[3]);
  29.  
  30.             if (contests.containsKey(contest)) {
  31.                 if (contests.get(contest).equals(password)) {
  32.  
  33.                     LinkedHashMap<String, Integer> course = new LinkedHashMap<>();
  34.                     course.put(contest, userPoints);
  35.  
  36.                     if (!users.containsKey(userName)) {
  37.                         users.put(userName, course);
  38.                     } else {
  39.                         if (!users.get(userName).containsKey(contest)) {
  40.                             users.get(userName).put(contest, userPoints);
  41.                         } else {
  42.                             users.get(userName).put(contest, Math.max(userPoints, users.get(userName).get(contest)));
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.             input = scanner.nextLine();
  48.         }
  49.         int totalSum = 0;
  50.         for (Map.Entry<String, LinkedHashMap<String, Integer>> user : users.entrySet()) {
  51.             int sum = user.getValue().values().stream().mapToInt(i -> i).sum();
  52.             if (sum > totalSum) {
  53.                 totalSum = sum;
  54.             }
  55.         }
  56.         for (Map.Entry<String, LinkedHashMap<String, Integer>> user : users.entrySet()) {
  57.             if (user.getValue().values().stream().mapToInt(i -> i).sum() == totalSum) {
  58.                 System.out.printf("Best candidate is %s with total %d points.%n", user.getKey(), totalSum);
  59.             }
  60.         }
  61.  
  62.         System.out.println("Ranking:");
  63.         users.forEach((key, value) -> {
  64.             System.out.printf("%s%n", key);
  65.             value.entrySet().stream().
  66.                     sorted((f, s) -> s.getValue() - f.getValue()).
  67.                     forEach(i -> System.out.printf("#  %s -> %d%n", i.getKey(), i.getValue()));
  68.         });
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement