我正在寻找一些帮助来转换一些代码,我必须使用真正漂亮的Java 8 Stream库。基本上我有一堆学生的对象,我想找回过滤对象的列表如下所示:使用Java筛选结果集的多个条件8
List<Integer> classRoomList;
Set<ScienceStudent> filteredStudents = new HashSet<>();
//Return only 5 students in the end
int limit = 5;
for (MathStudent s : mathStudents)
{
// Get the scienceStudent with the same id as the math student
ScienceStudent ss = scienceStudents.get(s.getId());
if (classRoomList.contains(ss.getClassroomId()))
{
if (!exclusionStudents.contains(ss))
{
if (limit > 0)
{
filteredStudents.add(ss);
limit--;
}
}
}
}
当然上面是一个超级做作的例子,我做了学习更多的Java的缘故8.假设所有学生都是从学生对象中延伸的,其中studentId
和classRoomId
。我需要的另一个要求是让结果成为不可变集合。
这是不是可以不实际的类和状态很容易地编译代码。你为什么不创建一个最小,紧凑和可验证的例子来说明你遇到的具体问题?有一个流过滤器的操作符,我认为它不会更清晰。 –