SHARE
TWEET

Untitled

shniaga Mar 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package AssociativeArraysExercise;
  2.  
  3. import java.util.*;
  4.  
  5. public class StudentAcademy {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.         Map<String, Double> students = new LinkedHashMap<>();
  11.         Map<String, Integer> numberOfGrades = new LinkedHashMap<>();
  12.  
  13.         for (int i = 0; i < n; i++) {
  14.             String name = scanner.nextLine();
  15.             double grade = Double.parseDouble(scanner.nextLine());
  16.  
  17.             if (!students.containsKey(name)) {
  18.                 students.put(name, grade);
  19.                 numberOfGrades.put(name, 1);
  20.             } else {
  21.                 students.put(name, students.get(name) + grade);
  22.                 numberOfGrades.put(name, numberOfGrades.get(name) + 1);
  23.             }
  24.         }
  25.         Map<String, Double> result = new LinkedHashMap<>();
  26.         for (String key : students.keySet()) {
  27.             if (numberOfGrades.containsKey(key)) {
  28.                 result.put(key, students.get(key) / numberOfGrades.get(key));
  29.             }
  30.         }
  31.         result.entrySet().stream()
  32.                 .filter(e -> e.getValue() >=4.50)
  33.                 .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  34.                 .forEach(e -> System.out.println(String.format("%s -> %.2f",e.getKey(),e.getValue())));
  35.     }
  36. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top