Advertisement
meteor4o

JF-Maps-Exercise-07.StudentAcademy

Jul 14th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class StudentAcademy {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         int n = Integer.parseInt(sc.nextLine());
  11.         Map<String, List<Double>> students = new LinkedHashMap<>();
  12.  
  13.         for (int i = 0; i < n; i++) {
  14.             String name = sc.nextLine();
  15.             double grade = Double.parseDouble(sc.nextLine());
  16.  
  17.             students.putIfAbsent(name, new ArrayList<>());
  18.             students.get(name).add(grade);
  19.  
  20.         }
  21.         Map<String, Double> grades = new LinkedHashMap<>();
  22.  
  23.         for (Map.Entry<String, List<Double>> entry : students.entrySet()) {
  24.             double avgGrade = entry.getValue().stream().mapToDouble(a->a).average().getAsDouble();
  25.  
  26.             if (avgGrade >= 4.5) {
  27.                 grades.put(entry.getKey(), avgGrade);
  28.             }
  29.         }
  30.  
  31.         grades.entrySet().stream().sorted((a,b) -> b.getValue().compareTo(a.getValue()))
  32.                 .forEach(student -> System.out.println(String.format("%s -> %.2f", student.getKey(), student.getValue())));
  33.  
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement