使用Java Stream流优化下面的代码不使用ifprivate ListEvaluationAnnexFileNameEnum checkSpecialfinal String templateVersionId final String empCode 特殊人员处理 final WorkProbitySpecialIdentityDTO specialIden
使用Java Stream流优化上述代码可以简化为以下方式:
private List<EvaluationAnnexFileNameEnum> checkSpecial(final String templateVersionId, final String empCode) {
// 特殊人员处理
final WorkProbitySpecialIdentityDTO specialIdentityDTO = evaluationParamService.getWorkProbitySpecialIdentity(templateVersionId);
if (Objects.isNull(specialIdentityDTO)) {
return Collections.emptyList();
}
List<EvaluationAnnexFileNameEnum> result = Stream.concat(
CollectionUtils.emptyIfNull(specialIdentityDTO.getOneLevelPrincipals())
.stream()
.map(SimpleEmployeeDTO::getCode)
.filter(item -> StringUtils.equals(item, empCode)),
CollectionUtils.emptyIfNull(specialIdentityDTO.getOneLevelDeputys())
.stream()
.map(SimpleEmployeeDTO::getCode)
.filter(item -> StringUtils.equals(item, empCode))
).findFirst()
.map(item -> StringUtils.equals(item, empCode) ? PRINCIPALS_UPLOAD_FILE_TYPES : DEPUTY_UPLOAD_FILE_TYPES)
.orElse(Collections.emptyList());
return result;
}
使用Stream流的concat()方法将两个Stream流合并,然后使用findFirst()方法获取第一个匹配的元素,如果存在匹配的元素,则根据条件返回相应的结果,否则返回空集合
原文地址: https://www.cveoy.top/t/topic/iHGt 著作权归作者所有。请勿转载和采集!