如何按Java

时间:2018-01-25 19:38:01

标签: 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);
        }
    }
}

此外,我需要使用线程来实现此并行,但这不是我现在主要关注的问题。 文件中的行设置如下:Joe, Doe, CS280, 92

2 个答案:

答案 0 :(得分:2)

您可以使用Collectors.groupingByCollectors.summarizingDouble获取DoubleSummaryStatistics,其中包括min,max,avg等。示例:

有:

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

List<Student> students = new ArrayList<>(Arrays.asList(
    new Student("student1", "student1", "course1", 90),
    new Student("student2", "student2", "course2", 86),
    new Student("student3", "student3", "course3", 92),
    new Student("student4", "student4", "course1", 80),
    new Student("student5", "student5", "course2", 93),
    new Student("student6", "student6", "course3", 78),
    new Student("student7", "student7", "course1", 95),
    new Student("student8", "student8", "course2", 83),
    new Student("student9", "student9", "course3", 79),
    new Student("student10", "student10.", "course1", 88)
));

你可以这样做:

Map<String, DoubleSummaryStatistics> statistics = students.stream()
        .collect(Collectors.groupingBy(Student::getCourseID,
                                       Collectors.summarizingDouble(Student::getScore)));

输出:

course3=DoubleSummaryStatistics{count=3, sum=249.000000, min=78.000000, average=83.000000, max=92.000000}, 
course2=DoubleSummaryStatistics{count=3, sum=262.000000, min=83.000000, average=87.333333, max=93.000000}, 
course1=DoubleSummaryStatistics{count=4, sum=353.000000, min=80.000000, average=88.250000, max=95.000000}

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

statistics.get("course1").getMin();
statistics.get("course2").getMax();

如果有关于此过程的performance concerns并且您希望它是并行的,则可以改为使用parallelStream

Map<String, DoubleSummaryStatistics> statistics = students.parallelStream()
        .collect(Collectors.groupingBy(Student::getCourseID,

答案 1 :(得分:1)

我这样做:

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

class CourseStat{
    private double bestScore;
    private double worstScore;

constructor ...gettersandsetters...
}

然后创建一个HashMap,其中键是courseID。基本上是这样的:

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,由课程Id索引,一个对象代表你学生的最差和最高分。