我正在尝试学习如何在Java 8中使用Streams,但是不确定如何在此处进行操作。

我有课程清单。我需要知道一个学期的所有课程是否都没有学生,如果是,那就做点什么。我想出了下面的代码,但这会在没有任何学生的任何课程被迭代时立即给出Null Pointer Exception。我需要知道如何纠正它:

List<Student> students = semester.getCourses().stream().flatMap(course -> course.getStudents().stream())
                .filter(Objects :: nonNull).collect(toList());
        if (CollectionUtils.isEmpty(students)){
            //cancel the semester or do something
        }

public class Semester{

 int semId;
 List<Course> courses;
}

public class Course{
 int courseId;
 List<Student> students;
}

最佳答案

在实际代码中,NullPointerException可能来自coursenullcourse.getStudents()null
此过滤器filter(Objects :: nonNull)是无奈的。它不会过滤null Student,这不是您的要求。
这段代码应该是您想要的:

List<Student> students =
 semester.getCourses()
         .stream()
         .filter(Objects::nonNull) // filter out null Course objects
         .map(Course::getStudents)
         .filter(Objects::nonNull) // filter out null Student List
         .flatMap(Collection::stream)
         .collect(toList());

还要注意,在所有地方添加空检查都不是一件好事:它会使“真实逻辑”的可读性降低。
您至少可以通过在它们的声明中将它们初始化来避免使用这些字段,例如:
public class Semester{
 int semId;
 List<Course> courses = new ArrayList<>();
}

public class Course{
 int courseId;
 List<Student> students = new ArrayList<>();
}

10-02 05:02
查看更多