Guest User

StudentAcademy

a guest
Mar 16th, 2019
336
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package students_grades;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import java.util.*;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.         int n = Integer.parseInt(scanner.nextLine());
  12.  
  13.         Map<String, List<Double>> students = new LinkedHashMap<>();
  14.  
  15.         for (int i = 0; i < n; i++) {
  16.             String input = scanner.nextLine();
  17.             double inputGrade = Double.parseDouble(scanner.nextLine());
  18.  
  19.             if (!students.containsKey(input)){
  20.  
  21.                 students.put(input,new ArrayList<>());
  22.             }
  23.             students.get(input).add(inputGrade);
  24.         }
  25.  
  26.         students.entrySet().stream()
  27.                 .map(e -> { //change the elements from Map.Entry<String, List<Double>> to Pair<String, Double>
  28.                     Double averageGrade = e.getValue().stream().mapToDouble(x -> x).average().getAsDouble();
  29.                     return new Pair<>(e.getKey(), averageGrade);
  30.                 }).filter(e -> e.getValue() >= 4.5) //remove all elements, which do NOT meet this criteria
  31.                 .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())) //sort descending
  32.                 .forEach(e -> System.out.println(String.format("%s -> %.2f", e.getKey(), e.getValue()))); //print
  33.     }
  34.  
  35. }
RAW Paste Data