如何在Java中按类的属性分组

如何在Java中按类的属性分组,java,Java,所以我有一个学生班,我必须从学生那里得到最好的成绩,但都是按照他们的课程进行分组的。基本上,我必须在每门课程中取得最好和最差的分数。所有内容都来自students.txt文件,这是我目前为止的代码 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.

所以我有一个学生班,我必须从学生那里得到最好的成绩,但都是按照他们的课程进行分组的。基本上,我必须在每门课程中取得最好和最差的分数。所有内容都来自students.txt文件,这是我目前为止的代码

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Main
{
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("src/students.txt");
        List<String> lines = Files.readAllLines(path);
        ArrayList<Student> students = new ArrayList<>();
        for (String line:lines) {
            String[] rawlines = line.split(",");
            String name  = rawlines[0];
            String lname  = rawlines[1];
            String courseID  = rawlines[2];
            String score = rawlines[3];
            double doublescore = Double.parseDouble(score);

            Student student = new Student(name, lname, courseID, doublescore);
            students.add(student);
        }

        for (Student s : students) {
            System.out.println(s);
        }
    }
}
import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.List;
公共班机
{
公共静态void main(字符串[]args)引发IOException{
Path=Path.get(“src/students.txt”);
列表行=文件。readAllLines(路径);
ArrayList students=新ArrayList();
用于(字符串行:行){
String[]rawlines=line.split(“,”);
字符串名称=粗线条[0];
字符串lname=rawlines[1];
字符串courseID=原始线[2];
字符串分数=粗线条[3];
double doublescore=double.parseDouble(分数);
学生=新学生(姓名、lname、课程ID、双倍分数);
学生。添加(学生);
}
(学生:学生){
系统输出打印项次;
}
}
}
我还需要使用线程来实现并行,但这不是我现在主要关心的问题。
文件中的行是这样设置的:
Joe,Doe,CS280,92

我会这样处理它:

创建一个类来封装您的最佳和最差分数,例如:

class CourseStat{
    private double bestScore;
    private double worstScore;

constructor ...gettersandsetters...
}
HashMap<String,CourseStat> courseMap = new HashMap<>();
for (Student s : students) {
    if(courseMap.contains(s.getCourseId())){
        CourseStat stat = courseMap.get(s.getCourseId());
        //Set the maximum and minimum score of the stat comparing the worst and best score with the current student.
        //...code goes here ...
    }
    else{
        //Create the course, add it to the map and set worst and best score to the current student's score.
        courseMap.put(s.getCourseId(),new CourseStat(s.getScore(),s.getScore()));
    }
}
然后创建一个HashMap,其中键是courseID。基本上是这样的:

class CourseStat{
    private double bestScore;
    private double worstScore;

constructor ...gettersandsetters...
}
HashMap<String,CourseStat> courseMap = new HashMap<>();
for (Student s : students) {
    if(courseMap.contains(s.getCourseId())){
        CourseStat stat = courseMap.get(s.getCourseId());
        //Set the maximum and minimum score of the stat comparing the worst and best score with the current student.
        //...code goes here ...
    }
    else{
        //Create the course, add it to the map and set worst and best score to the current student's score.
        courseMap.put(s.getCourseId(),new CourseStat(s.getScore(),s.getScore()));
    }
}
HashMap courseMap=newhashmap();
(学生:学生){
if(courseMap.contains(s.getCourseId())){
CourseStat stat=courseMap.get(s.getCourseId());
//设置统计的最大和最小分数,将最差和最佳分数与当前学生进行比较。
//…代码在这里。。。
}
否则{
//创建课程,将其添加到地图中,并将最差和最佳分数设置为当前学生的分数。
courseMap.put(s.getCourseId(),new CourseStat(s.getScore(),s.getScore());
}
}
然后,您将拥有一个hashmap,由课程Id索引,以及一个表示学生最差和最好成绩的对象。

您可以使用并获得一个包含最小值、最大值、平均值等的值。示例:

具有:

class Student{
    private String name;
    private String lname;
    private String courseID;
    private double score;
    // getters 'n setters, constructors, toString, etc
}

这里有一份学生名单。我们按照课程ID对学生进行分组,然后根据分数得到每门课程的统计数据。然后我们可以得到每个课程的值,如:

statistics.get("course1").getMin();
statistics.get("course2").getMax();
如果有关于此过程的问题,并且您希望它是并行的,则可以使用:

Map statistics=students.parallelStream()
.collect(收集器).groupingBy(学生::getCourseID,

你的意思是你只需要在每次课程中获得最差和最好的成绩,还是你的意思是你需要按课程id,然后按成绩对学生名单进行排序?这里没有。不要随意标记。
statistics.get("course1").getMin();
statistics.get("course2").getMax();
Map<String, DoubleSummaryStatistics> statistics = students.parallelStream()
        .collect(Collectors.groupingBy(Student::getCourseID,