使用Java Stream流优化下面的代码需要去除过多的ifprivate ListEvaluationAnnexFileNameEnum checkSpecialfinal String templateVersionId final String empCode 特殊人员处理 final WorkProbitySpecialIdentityDTO special
使用Java Stream流优化后的代码如下所示:
private List
List<EvaluationAnnexFileNameEnum> result = Stream.concat(
specialIdentityDTO.getOneLevelPrincipals().stream().map(SimpleEmployeeDTO::getCode),
specialIdentityDTO.getOneLevelDeputys().stream().map(SimpleEmployeeDTO::getCode))
.filter(item -> StringUtils.equals(item, empCode))
.map(item -> {
if (specialIdentityDTO.getOneLevelPrincipals().stream().anyMatch(principal -> StringUtils.equals(principal.getCode(), empCode))) {
return PRINCIPALS_UPLOAD_FILE_TYPES;
} else if (specialIdentityDTO.getOneLevelDeputys().stream().anyMatch(deputy -> StringUtils.equals(deputy.getCode(), empCode))) {
return DEPUTY_UPLOAD_FILE_TYPES;
} else {
return null;
}
})
.filter(Objects::nonNull)
.findFirst()
.orElse(Collections.emptyList());
return result;
原文地址: https://www.cveoy.top/t/topic/iHGj 著作权归作者所有。请勿转载和采集!