【发布时间】:2017-05-11 19:30:43
【问题描述】:
我有嵌套列表,如果条件为真,我可以设置 isMatched 和 department.setMatchedStatus(true)。
boolean isMatched = false;
for (Employee employee: company.getEmployees()) {
for (Department department: employee.getDepartments()) {
if(departmentList.contains(department.getDepartmentName())){
isMatched = true;
department.setMatchedStatus(true);
}
}
}
return isMatched;
希望使用 java 8 流实现相同的功能,我尝试使用以下代码,但无法返回布尔值。
isMatched = company.getEmployees().stream()
.flatMap(employee-> employee.getDepartments().stream())
.filter((department) -> departmentList.contains(department.getDepartmentName()))
.forEach((department) -> department.setMatchedStatus(true));
有人可以帮我解决这个问题吗?
【问题讨论】:
-
您的原始代码也不返回 bool。你只需设置属性,stream() 代码也一样。
-
对不起,其实我错过了,我只是编辑。
标签: java java-8 java-stream