优化这段代码增加代码的可阅读性和简洁性并给出示例 for (TaskRuleDetail primaryDatum : primaryData) { String taskCode = primaryDatum.getTaskCode(); String date = primaryDatum.getDataField(); String rate = primaryDatum.getDataValue(); if (CommonBusinessEnum.RuleDetailDataFieldEnum.USER_IDENTITY.getValue().equals(date)) { if (!user.toString().contains(rate)) { user.append(rate).append(","); } continue; } if (CommonBusinessEnum.RuleDetailDataFieldEnum.APP_CODE.getValue().equals(date)) { if (!app.toString().contains(rate)) { app.append(rate).append(","); } continue; } //若临时map中不包含这一客户名称和编号,则创建新的valueMap来存放结果对象 if (!lamp.containsKey(taskCode)) { valueMap = new HashMap<>(); valueMap.put("taskCode", taskCode); valueMap.put(date, rate); //根据任务id 来查询奖励规则表 //查询奖励相关参数 String type = task.getType(); List<TaskAward> taskAward = taskAwardService.selectTaskAwardByCode(primaryDatum.getTaskCode(), taskGroupCode); if(!CollectionUtils.isEmpty(taskAward)){ if(type.equals(CommonBusinessEnum.TaskTypeEnum.YDWZ.getValue())){ List<AwardDetails> objects = taskAward.stream() .map(award -> { AwardDetails awardDetails = new AwardDetails(); awardDetails.setAwardName(award.getName()); awardDetails.setAwardInfo(award.getInfo()); awardDetails.setAwardType(award.getAwardType()); return awardDetails; }) .collect(Collectors.toList()); valueMap.put("awardDetail", objects); }else { StringBuilder sbName = new StringBuilder(); StringBuilder sbInfo = new StringBuilder(); for (TaskAward award : taskAward) { sbName.append(award.getName()).append(","); sbInfo.append(award.getInfo()).append(","); } String awardName = sbName.deleteCharAt(sbName.length() - 1).toString(); String awardInfo = sbInfo.deleteCharAt(sbInfo.length() - 1).toString(); valueMap.put("awardType", taskAward.get(0).getAwardType()); valueMap.put("awardName", awardName); valueMap.put("awardInfo", awardInfo); } } //将新建的map对象放入临时对象中,用于下一次的判断 lamp.put(taskCode, valueMap); result.add(valueMap); } else { //若临时map中包含这一客户名称和编号,则在已有的基础上将日期与准交率添加进去 valueMap = lamp.get(taskCode); valueMap.put(date, rate); } }
时间: 2023-02-06 20:25:22 浏览: 207
code-examples:扁平化代码示例
下面是优化后的代码示例,在保证功能不变的情况下尽量增加代码的可阅读性和简洁性:
```
List<Map<String, Object>> result = new ArrayList<>();
Map<String, Map<String, Object>> lamp = new HashMap<>();
StringBuilder user = new StringBuilder();
StringBuilder app = new StringBuilder();
for (TaskRuleDetail primaryDatum : primaryData) {
// 获取当前遍历数据的任务编号、数据字段、数据值
String taskCode = primaryDatum.getTaskCode();
String dataField = primaryDatum.getDataField();
String dataValue = primaryDatum.getDataValue();
// 特殊处理 USER_IDENTITY 和 APP_CODE
if (CommonBusinessEnum.RuleDetailDataFieldEnum.USER_IDENTITY.getValue().equals(dataField)) {
if (!user.toString().contains(dataValue)) {
user.append(dataValue).append(",");
}
continue;
}
if (CommonBusinessEnum.RuleDetailDataFieldEnum.APP_CODE.getValue().equals(dataField)) {
if (!app.toString().contains(dataValue)) {
app.append(dataValue).append(",");
}
continue;
}
// 获取当前任务的类型
String type = task.getType();
// 查询当前任务的奖励规则
List<TaskAward> taskAward = taskAwardService.selectTaskAwardByCode(taskCode, taskGroupCode);
// 若临时 map 中不包含这一客户名称和编号,则创建新的 valueMap 来存放结果对象
Map<String, Object> valueMap;
if (!lamp.containsKey(taskCode)) {
valueMap = new HashMap<>();
valueMap.put("taskCode", taskCode);
valueMap.put(dataField, dataValue);
// 处理奖励信息
if (!CollectionUtils.isEmpty(taskAward)) {
if (type.equals(CommonBusinessEnum.TaskTypeEnum.YDWZ.getValue())) {
List<AwardDetails> objects = taskAward.stream()
.map(award -> {
AwardDetails awardDetails = new AwardDetails();
awardDetails.setAwardName(award.getName());
awardDetails.setAwardInfo(aw
阅读全文