Maven: <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>1.6.0</version> </dependency> </dependencies> Gradle: groovy dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0" } public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } } import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class GradeCalculator { private List<Student> students; public GradeCalculator() { students = new ArrayList<>(); } public void addStudent(Student student) { students.add(student); } public double calculateAverageScore() { return students.stream() .mapToInt(Student::getScore) .average() .orElse(0); } public int getHighestScore() { return students.stream() .mapToInt(Student::getScore) .max() .orElse(0); } public int getLowestScore() { return students.stream() .mapToInt(Student::getScore) .min() .orElse(0); } public List<Student> getStudentsAboveScore(int score) { return students.stream() .filter(student -> student.getScore() > score) .collect(Collectors.toList()); } } public class Main { public static void main(String[] args) { GradeCalculator calculator = new GradeCalculator(); double averageScore = calculator.calculateAverageScore(); int highestScore = calculator.getHighestScore(); int lowestScore = calculator.getLowestScore(); List<Student> above90Students = calculator.getStudentsAboveScore(90); } }


上一篇:
下一篇:
切换中文