Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8 坚持使用Java8流_Java 8_Java Stream - Fatal编程技术网

Java 8 坚持使用Java8流

Java 8 坚持使用Java8流,java-8,java-stream,Java 8,Java Stream,我正在努力学习如何在Java8中使用流,但不知道如何在这里进行 我有一份课程表。我需要知道一个学期的所有课程是否都没有学生,如果是这样,做点什么。我想出了下面的代码,但这是给空指针异常,只要任何课程得到迭代,没有任何学生。我需要知道如何才能纠正它: List<Student> students = semester.getCourses().stream().flatMap(course -> course.getStudents().stream())

我正在努力学习如何在Java8中使用流,但不知道如何在这里进行

我有一份课程表。我需要知道一个学期的所有课程是否都没有学生,如果是这样,做点什么。我想出了下面的代码,但这是给空指针异常,只要任何课程得到迭代,没有任何学生。我需要知道如何才能纠正它:

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;
}
List students=sement.getCourses().stream().flatMap(课程->课程.getStudents().stream())
.filter(Objects::nonNull).collect(toList());
if(CollectionUtils.isEmpty(学生)){
//取消学期或者做点什么
}
公开课学期{
int-semId;
列出课程;
}
公共课{
int courseId;
列出学生名单;
}
我认为这足以满足你的要求


注意:它可能会得到编译错误,因为我没有使用编辑器工具。

在实际代码中,
NullPointerException
可能来自
course
is
null
course.getStudents()
is
null

此筛选器
筛选器(Objects::nonNull)
无效。它不会过滤
null
Student
s,这不是您的要求。
此代码应该是您正在查找的代码:

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());
列出学生=
学期课程
.stream()
.filter(Objects::nonNull)//过滤掉空的课程对象
.map(课程::getStudents)
.filter(Objects::nonNull)//筛选出空的学生列表
.flatMap(集合::流)
.collect(toList());
还要注意的是,到处添加空检查并不好:它会降低“真实逻辑”的可读性。
您至少可以通过在集合字段的声明中初始化这些字段来避免它们,例如:

public class Semester{
 int semId;
 List<Course> courses = new ArrayList<>();
}

public class Course{
 int courseId;
 List<Student> students = new ArrayList<>();
}
公共课学期{
int-semId;
列出课程=新建ArrayList();
}
公共课{
int courseId;
List students=new ArrayList();
}

So。。您想知道这些课程是否都符合getStudents()为空的条件?是的。我需要知道所有课程中是否都没有学生。是的,我在暗示答案,你只需在答案中添加正确的标点符号即可……:)我需要知道所有课程加起来是否都没有学生。我不知道;i don’我不想知道这些课程是否有零名学生,我需要知道是否有零名学生。假设这个学期有0门课程,那么sement.getCourses().stream()将给出NullPointer异常,那么在这之后.filter(Objects::nonNull)又有什么用呢?如果这学期有2门课,只有它们会被流式处理,而不是空的。我只是想了解第一个“过滤器”(Objects::nonNull)的用法。空但不为null的
Iterable
不会产生
NullPointerException
。只是流操作不需要任何处理。这就像用一个空的
Iterable
进行迭代:不会引发异常。我更新了评论,明白了。非常感谢。
public class Semester{
 int semId;
 List<Course> courses = new ArrayList<>();
}

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