我想的foreach轉換成流JAVA的foreach到流
for (Entity entity : listOfEntitys.getAll()) {
if (!isEntityDateValid(entity.getStartDate())
|| !isEntityDateValid(entity.getEndDate())) {
return false;
}
}
所以我將它轉換這樣
if (listOfEntitys.getAll() != null) {
return listOfEntitys.getAll().stream().anyMatch(entity-> !isEntityDateValid(entity.getStartDate())
|| !isEntityDateValid(entity.getEndDate()));
}
但我搞砸了,因爲它的結果總是布爾值和我只在滿足條件時才返回它
條件返回語句不能更改爲純流,但如果您向我們展示更多代碼,我們可能會幫助您 – ByeBye
無需空檢查,因爲如果getAll返回null,for-each也會失敗。 – john16384