for (String type : taskType) { Integer approvalType = null; if (type.equals(EcProjectTaskTypeEnums.LEAVE.getCode())){ approvalType = 1; }else if (type.equals(EcProjectTaskTypeEnums.GO_OUT.getCode())){ approvalType = 3; }else if (type.equals(EcProjectTaskTypeEnums.ON_BUSINESS.getCode())){ approvalType = 4; }else if (type.equals(EcProjectTaskTypeEnums.OVERTIME.getCode())){ approvalType = 5; } Integer finalApprovalType = approvalType; List<UserApprovalVO> approvalVOS = value.stream().filter(u -> u.getType() == (finalApprovalType)).collect(Collectors.toList()); result.addAll(approvalVOS); 优化以上代码
时间: 2024-04-15 11:27:31 浏览: 140
在写算法之前首先需定义栈的元素类型-树和二叉树的应用
您可以尝试使用一个`Map`来存储任务类型与对应的审批类型的映射关系,这样可以避免多次的条件判断。
可以按照以下步骤进行优化:
1. 创建一个`Map`来存储任务类型与审批类型的映射关系:
```java
Map<String, Integer> typeApprovalMap = new HashMap<>();
typeApprovalMap.put(EcProjectTaskTypeEnums.LEAVE.getCode(), 1);
typeApprovalMap.put(EcProjectTaskTypeEnums.GO_OUT.getCode(), 3);
typeApprovalMap.put(EcProjectTaskTypeEnums.ON_BUSINESS.getCode(), 4);
typeApprovalMap.put(EcProjectTaskTypeEnums.OVERTIME.getCode(), 5);
```
2. 使用`typeApprovalMap`来获取任务类型对应的审批类型:
```java
for (String type : taskType) {
Integer approvalType = typeApprovalMap.get(type);
// 其他代码...
}
```
3. 将筛选和收集结果的代码简化为一行,使用`Stream`的`filter`和`flatMap`操作:
```java
result.addAll(taskType.stream()
.map(typeApprovalMap::get)
.flatMap(approvalType -> value.stream()
.filter(u -> u.getType() == approvalType))
.collect(Collectors.toList()));
```
这样,就能够优化以上代码并且使用更简洁的方式实现相同的功能。
阅读全文