deyanmalinov

05. Average Students Grades

May 28th, 2019
147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.     public static void main(String[] args){
  8.         Scanner scan = new Scanner(System.in);
  9.         TreeMap<String, List<Double>> students = new TreeMap<>();
  10.         int num = Integer.parseInt(scan.nextLine());
  11.  
  12.         for (int i = 0; i < num; i++) {
  13.             String[] line = scan.nextLine().split(" ");
  14.             String name = line[0];
  15.             double grade = Double.parseDouble(line[1]);
  16.             students.putIfAbsent(name, new ArrayList<>());
  17.             students.get(name).add(grade);
  18.  
  19.         }
  20.         students.forEach((name, grades) ->{
  21.             String allGrades = grades.stream()
  22.                     .map(g -> String.format("%.2f", g))
  23.                     .collect(Collectors.joining(" "));
  24.             double avg = grades.stream().mapToDouble(e -> e).average().orElse(0d);
  25.             System.out.println(String.format("%s -> %s (avg: %.2f)", name, allGrades, avg));
  26.         });
  27.     }
  28. }
RAW Paste Data