SHARE
TWEET

StudentAcademy

elvirynwa Mar 18th, 2019 364 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class StudentAcademy {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, List<Double>> students = new LinkedHashMap<>();
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             String studentName = scanner.nextLine();
  12.             double grade = Double.parseDouble(scanner.nextLine());
  13.  
  14.             if (!students.containsKey(studentName)) {
  15.                 students.put(studentName, new ArrayList<>());
  16.                 students.get(studentName).add(grade);
  17.             } else {
  18.                 students.get(studentName).add(grade);
  19.             }
  20.         }
  21.  
  22.         Map<String, Double> averageGrade = new LinkedHashMap<>();
  23.  
  24.         for (Map.Entry<String, List<Double>> kvp : students.entrySet()) {
  25.             double average = kvp.getValue().stream().mapToDouble(x -> x).average().getAsDouble();
  26.             if (average >= 4.50) {
  27.                 averageGrade.put(kvp.getKey(), average);
  28.             }
  29.         }
  30.  
  31.         averageGrade.entrySet().stream()
  32.                 .sorted((g1, g2) -> g2.getValue().compareTo(g1.getValue()))
  33.                 .forEach(grade -> {
  34.                     System.out.println(String.format("%s -> %.2f", grade.getKey(), grade.getValue()));
  35.                 });
  36.     }
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top