我希望用Java lambda表达式替换下面的代码。在下面的例子中,我需要在找到匹配时出现循环并将boolean设置为true以断言条件
Long teamId;
boolean matchFound = false;
List<WorkflowSubscriptions> workflowSubscriptionsList = fetchSubscriptions();
for (WorkflowSubscriptions workflowSubscriptions : workflowSubscriptionsList) {
for (WorkflowCompositeInfo workflowCompositeInfo : workflowSubscriptions.getWorkflowCompositeInfo()) {
if (workflowCompositeInfo.getId() > 0 && workflowCompositeId == workflowCompositeId.intValue()) {
teamId = Long.valueOf(workflowSubscriptions.getId());
matchFound = true;
}
}
}
if (!matchFound) {
throw new CustomParameterizedException("Workflow does not have valid subscriptions");
}
答案 0 :(得分:1)
试试这个:
fetchSubscriptions().stream()
.filter(p -> w.getId() > 0 && workflowCompositeId == w.intValue())
.limit(1)
.forEach(w -> {teamId = Long.valueOf(w); matchFound = true;} );
<强>更新强>
避免修改最终变量的错误。您只需创建一个值类:
public class MyValue {
public boolean matchFound;
public Long teamId;
}
然后:
final MyValue value = new MyValue();
fetchSubscriptions().stream()
.filter(p -> w.getId() > 0 && workflowCompositeId == w.intValue())
.limit(1)
.forEach(w -> {value.teamId = Long.valueOf(w); value.matchFound = true;} );
// Here, find your data in value.teamId and value.matchFound