我正在尋找一些幫助來轉換一些代碼,我必須使用真正漂亮的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
。我需要的另一個要求是讓結果成爲不可變集合。
這是不是可以不實際的類和狀態很容易地編譯代碼。你爲什麼不創建一個最小,緊湊和可驗證的例子來說明你遇到的具體問題?有一個流過濾器的操作符,我認爲它不會更清晰。 –