优化一下代码:String taskType = null; if (key == VacationTypeEnum.TYPE1.getCode()){ taskType = EcProjectTaskTypeEnums.LEAVE.getCode(); }else if (key == VacationTypeEnum.TYPE3.getCode()){ taskType = EcProjectTaskTypeEnums.ON_BUSINESS.getCode(); }else if (key == VacationTypeEnum.TYPE4.getCode()){ taskType = EcProjectTaskTypeEnums.GO_OUT.getCode(); }else if (key == VacationTypeEnum.TYPE5.getCode()){ taskType = EcProjectTaskTypeEnums.OVERTIME.getCode(); }
时间: 2024-02-14 15:29:01 浏览: 92
您可以使用一个`Map`来存储假期类型与任务类型的映射关系,以避免多次的条件判断。
可以按照以下步骤进行优化:
1. 创建一个`Map`来存储假期类型与任务类型的映射关系:
```java
Map<Integer, String> vacationTaskMap = new HashMap<>();
vacationTaskMap.put(VacationTypeEnum.TYPE1.getCode(), EcProjectTaskTypeEnums.LEAVE.getCode());
vacationTaskMap.put(VacationTypeEnum.TYPE3.getCode(), EcProjectTaskTypeEnums.ON_BUSINESS.getCode());
vacationTaskMap.put(VacationTypeEnum.TYPE4.getCode(), EcProjectTaskTypeEnums.GO_OUT.getCode());
vacationTaskMap.put(VacationTypeEnum.TYPE5.getCode(), EcProjectTaskTypeEnums.OVERTIME.getCode());
```
2. 使用`vacationTaskMap`来获取假期类型对应的任务类型:
```java
String taskType = vacationTaskMap.get(key);
```
这样,就能够优化以上代码并且使用更简洁的方式实现相同的功能。
相关问题
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); 优化以上代码
您可以尝试使用一个`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()));
```
这样,就能够优化以上代码并且使用更简洁的方式实现相同的功能。
render: (h, params) => { let titleStr = "发布任务"; if (params.row.tasktype == "项目任务") { titleStr = "指定流程模板"; } var settingBtn = h( "Button", { props: { type: "info", size: "small", disabled: params.row.tasktype == "项目任务" && Number(params.row.status) > 1, }, attrs: { title: titleStr, }, style: { marginRight: "2px", border: "none", color: params.row.tasktype == "项目任务" && Number(params.row.status) > 1 ? "" : "#1884FF", background: "none", }, on: { click: () => { this.showProjectSettings(params); }, }, }, "配置" );将配置改成图标
可以将配置按钮改成图标,比如使用 Element UI 中的 Icon 组件,代码如下:
```
var settingBtn = h(
"el-button",
{
props: {
type: "info",
size: "small",
disabled:
params.row.tasktype == "项目任务" &&
Number(params.row.status) > 1,
},
attrs: {
title: titleStr,
},
style: {
marginRight: "2px",
border: "none",
color:
params.row.tasktype == "项目任务" &&
Number(params.row.status) > 1
? ""
: "#1884FF",
background: "none",
},
on: {
click: () => {
this.showProjectSettings(params);
},
},
},
[
h("i", { class: "el-icon-setting", style: { marginRight: "5px" } }),
"配置",
]
);
```
这样就可以将配置按钮改成了带有设置图标的按钮。
阅读全文