Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class _05_AverageStudentsGrades {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- TreeMap<String, List<Double>> students = new TreeMap<>();
- int n = Integer.parseInt(scan.nextLine());
- while (n-- > 0) {
- String[] tokens = scan.nextLine().split("\\s+");
- String student = tokens[0];
- double grade = Double.parseDouble(tokens[1]);
- students.putIfAbsent(student, new ArrayList<>());
- students.get(student).add(grade);
- }
- students.entrySet()
- .stream()
- .forEach(entry -> {
- double sum = 0;
- for (int i = 0; i < entry.getValue().size(); i++) {
- sum += entry.getValue().get(i);
- }
- double avgSum = sum / entry.getValue().size();
- System.out.print(entry.getKey() + " -> ");
- entry.getValue().forEach(e -> System.out.printf("%.2f ", e));
- System.out.printf("(avg: %.2f)%n", avgSum);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement