Advertisement
RGAlex

O2. Judge / Maps, Lambda and Stream API - More Exercise

Mar 27th, 2021
149
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. import java.util.HashMap;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class O2Judge {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         Map<String , Map<String , Integer>> courseInfo = new LinkedHashMap<>();
  11.  
  12.         String input = scanner.nextLine();
  13.         while (!input.equals("no more time")){
  14.             String[] tokens = input.split(" -> ");
  15.             String username = tokens[0];
  16.             String contest = tokens[1];
  17.             int points = Integer.parseInt(tokens[2]);
  18.             courseInfo.putIfAbsent(contest , new LinkedHashMap<>());
  19.             courseInfo.get(contest).putIfAbsent(username , 0);
  20.             if (courseInfo.get(contest).get(username) < points){
  21.                 courseInfo.get(contest).put(username , points);
  22.             }
  23.             input = scanner.nextLine();
  24.         }
  25.         int[] num = new int[1];
  26.         courseInfo
  27.                 .forEach((key, value) -> {
  28.                     num[0] = 0;
  29.                     System.out.printf("%s: %d participants%n", key, value.size());
  30.                     value
  31.                             .entrySet()
  32.                             .stream()
  33.                             .sorted((s1, s2) -> {
  34.                                 int result = s2.getValue().compareTo(s1.getValue());
  35.                                 if (result == 0) {
  36.                                     result = s1.getKey().compareTo(s2.getKey());
  37.                                 }
  38.                                 return result;
  39.                             })
  40.                             .forEach(s -> System.out.printf("%d. %s <::> %d%n", ++num[0], s.getKey(), s.getValue()));
  41.                 });
  42.         Map<String , Integer> studentInfo = new HashMap<>();
  43.         for (Map.Entry<String, Map<String , Integer>> current : courseInfo.entrySet()){
  44.             for (Map.Entry<String, Integer> student: current.getValue().entrySet()){
  45.                 studentInfo.putIfAbsent(student.getKey() , 0);
  46.                 int newPoints = studentInfo.get(student.getKey()) + student.getValue();
  47.                 studentInfo.put(student.getKey() , newPoints);
  48.             }
  49.         }
  50.         System.out.println("Individual standings:");
  51.         studentInfo
  52.                 .entrySet()
  53.                 .stream()
  54.                 .sorted((s1,s2) -> {
  55.                     num[0] = 0;
  56.                     int result = s2.getValue().compareTo(s1.getValue());
  57.                     if ( result == 0){
  58.                         result = s1.getKey().compareTo(s2.getKey());
  59.                     }
  60.                     return result;
  61.                 })
  62.                 .forEach(s -> System.out.printf("%d. %s -> %d%n", ++num[0] , s.getKey() , s.getValue()));
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement