我正在尝试根据某些条件在原始列表的顶部提取过滤列表。我正在使用Java 8的backport版本,我不太清楚如何做到这一点。我从ccarReport.getCcarReportWorkflowInstances()调用得到Set。我需要根据条件匹配来迭代和过滤这个集合(我将每个对象中的日期属性与传递的请求日期进行比较。下面是代码
public List<CcarRepWfInstDTO> fetchReportInstances(Long reportId, Date cobDate) {
List<CcarRepWfInstDTO> ccarRepWfInstDTOs = null;
CcarReport ccarReport = validateInstanceSearchParams(reportId, cobDate);
Set<CcarReportWorkflowInstance> ccarReportWorkflowInstanceSet = ccarReport.getCcarReportWorkflowInstances();
List<CcarReportWorkflowInstance> ccarReportWorkflowInstances = StreamSupport.stream(ccarReportWorkflowInstanceSet).filter(ccarReportWorkflowInstance -> DateUtils.isSameDay(cobDate, ccarReportWorkflowInstance.getCobDate()));
ccarRepWfInstDTOs = ccarRepWfInstMapper.ccarRepWfInstsToCcarRepWfInstDTOs(ccarReportWorkflowInstances);
return ccarRepWfInstDTOs;
}
正在完成工作的例程
{{1}}
我尝试使用流时出错。
答案 0 :(得分:2)
假设我理解你要做的事情,你可以用一行替换你的方法体:
return
validateInstanceSearchParams(reportId, cobDate).getCcarReportWorkflowInstances()
.stream()
.filter(c -> DateUtils.isSameDay(cobDate, c.getCobDate()))
.collect(Collectors.toList());
stream()
方法从Set中获取Stream。无需StreamSupport.stream()
。collect
将其导入输出List
。