RGAlex

O7. Student Academy / Maps, Lambda and Stream API - Exercise

Mar 8th, 2021 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package MapsLambdaStreamAPI.Exercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class O7StudentAcademy {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, Double> students = new LinkedHashMap<>();
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             String studentName = scanner.nextLine();
  14.             double grade = Double.parseDouble(scanner.nextLine());
  15.  
  16.             if (!students.containsKey(studentName)) {
  17.                 students.put(studentName, grade);
  18.             } else {
  19.                 Double average = ((students.get(studentName) + grade) / 2);
  20.                 students.put(studentName, average);
  21.             }
  22.         }
  23.         students.entrySet()
  24.                 .stream()
  25.                 .filter(a->a.getValue()>=4.5)
  26.                 .sorted((g1, g2) -> g2.getValue().compareTo(g1.getValue()))
  27.                 .forEach(grade -> {
  28.                     System.out.printf("%s -> %.2f%n", grade.getKey(), grade.getValue());
  29.                 });
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment