优化以下代码 TaskGroupListDetailDTO detailDTO = new TaskGroupListDetailDTO(); detailDTO.setUserIdentity(userIdentity); detailDTO.setAppCode(appCode); detailDTO.setTaskName(taskName); detailDTO.setType(type); CommonBusinessEnum.TaskTypeEnum taskTypeEnum = CommonBusinessEnum.TaskTypeEnum.ofByValue(type); if (taskTypeEnum != null) { detailDTO.setTaskTypeName(taskTypeEnum.getName()); } detailDTO.setTaskValidTime(taskValidTime); detailDTO.setTaskInvalidTime(taskInvalidTime); detailDTO.setTaskPurpose(taskPurpose); detailDTO.setChannel(channel); detailDTO.setDepartment(department); detailDTO.setTaskInfo(taskInfo); detailDTO.setTaskState(taskState); Map<String, Object> stringObjectMap = result.get(0); Object duration = stringObjectMap.get("reading_article_duration"); if (duration == null) { detailDTO.setDataList(result); } else { Object readingArticleCountMax = stringObjectMap.get("reading_article_count_max"); Object readingArticlePlatformShared = stringObjectMap.get("reading_article_platform_shared"); Object repeatRewards = stringObjectMap.get("repeat_rewards"); Object dayCompleteTask = stringObjectMap.get("day_complete_task_max"); Object awardType = stringObjectMap.get("awardType"); Object awardName = stringObjectMap.get("awardName"); Object awardInfo = stringObjectMap.get("awardInfo"); detailDTO.setReadingArticleDuration(Long.valueOf((String) duration)); detailDTO.setReadingArticleCountMax(Integer.valueOf((String) readingArticleCountMax)); detailDTO.setReadingArticlePlatformShared(Boolean.valueOf((String) readingArticlePlatformShared)); detailDTO.setRepeatRewards(Boolean.valueOf((String) repeatRewards)); detailDTO.setDayCompleteTask(Integer.valueOf((String) dayCompleteTask)); detailDTO.setAwardType((Integer) awardType); detailDTO.setAwardName((String) awardName); detailDTO.setAwardInfo((String) awardInfo); detailDTO.setDataList(new ArrayList<>()); } return detailDTO;
时间: 2023-02-13 10:27:49 浏览: 124
在web.config和app.config文件中增加自定义配置节点的方法
下面是对上述代码的优化建议:
1. 使用基本数据类型,而不是使用包装类型(例如,使用 `int` 而不是 `Integer`),以减少内存占用。
2. 使用类型转换函数(例如,`Integer.parseInt`)来转换字符串到基本数据类型。
3. 使用 `Map.getOrDefault` 方法,可以避免在 `Map` 中进行重复查找。
例如:
```
Map<String, Object> stringObjectMap = result.get(0);
detailDTO.setReadingArticleDuration(Long.parseLong((String) stringObjectMap.getOrDefault("reading_article_duration", 0)));
detailDTO.setReadingArticleCountMax(Integer.parseInt((String) stringObjectMap.getOrDefault("reading_article_count_max", 0)));
detailDTO.setReadingArticlePlatformShared(Boolean.parseBoolean((String) stringObjectMap.getOrDefault("reading_article_platform_shared", false)));
detailDTO.setRepeatRewards(Boolean.parseBoolean((String) stringObjectMap.getOrDefault("repeat_rewards", false)));
detailDTO.setDayCompleteTask(Integer.parseInt((String) stringObjectMap.getOrDefault("day_complete_task_max", 0)));
detailDTO.setAwardType((Integer) stringObjectMap.getOrDefault("awardType", 0));
detailDTO.setAwardName((String) stringObjectMap.getOrDefault("awardName", ""));
detailDTO.setAwardInfo((String) stringObjectMap.getOrDefault("awardInfo", ""));
```
4. 将所有对 `TaskGroupListDetailDTO` 对象的赋值操作放在一起,可以使代码更加紧凑。
例如:
```
TaskGroupListDetailDTO detailDTO = new TaskGroupListDetailDTO();
detailDTO.setUserIdentity(userIdentity);
detailDTO.setAppCode(appCode);
detailDTO.setTaskName(taskName);
detailDTO.setType(type);
CommonBusinessEnum.TaskTypeEnum taskTypeEnum = CommonBusinessEnum.TaskTypeEnum.ofByValue(type);
if (taskTypeEnum != null) {
detailDTO.setTaskTypeName(taskTypeEnum.getName());
}
detailDTO.setTaskValid
阅读全文