使用steam流优化public ListInteger findIdentitiesfinal String empCode final ListInteger roles = ListsnewArrayList; if activityAppraiseeServiceexistByEmpCodeempCode rolesaddUserTyp
为了优化这段代码,我们可以使用流(Stream)来简化操作。
首先,我们可以使用Stream.of()方法创建一个包含UserTypeEnum.APPRAISEE.getValue()和UserTypeEnum.EXAMINER.getValue()的流。然后,我们可以使用filter()方法过滤流中存在对应empCode的元素。最后,我们可以使用collect()方法将流中的元素收集到一个新的列表中。
下面是优化后的代码:
public List<Integer> findIdentities(final String empCode) {
return Stream.of(UserTypeEnum.APPRAISEE.getValue(), UserTypeEnum.EXAMINER.getValue())
.filter(role -> {
if (role == UserTypeEnum.APPRAISEE.getValue()) {
return activityAppraiseeService.existByEmpCode(empCode);
} else if (role == UserTypeEnum.EXAMINER.getValue()) {
return activityExaminerService.existByEmpCode(empCode);
} else {
return false;
}
})
.collect(Collectors.toList());
}
这样,我们就使用流来简化了代码,同时保持了原有的逻辑
原文地址: http://www.cveoy.top/t/topic/hCp2 著作权归作者所有。请勿转载和采集!