优化这段代码:if (constructionCompanyVo.getProjectCodeList() != null && constructionCompanyVo.getProjectCodeList().size() > 0) { constructionCompany.setProjectCode(String.join(",", constructionCompanyVo.getProjectCodeList())); constructionCompany.setProjectName(String.join(",", constructionCompanyVo.getProjectNameList())); }
时间: 2024-02-27 15:56:35 浏览: 48
可以优化的地方有:
1. 使用 Optional 类简化代码:
```
Optional.ofNullable(constructionCompanyVo.getProjectCodeList())
.filter(list -> !list.isEmpty())
.ifPresent(list -> {
constructionCompany.setProjectCode(String.join(",", list));
constructionCompany.setProjectName(String.join(",", constructionCompanyVo.getProjectNameList()));
});
```
2. 使用 StringUtils 类简化代码:
```
if (ArrayUtils.isNotEmpty(constructionCompanyVo.getProjectCodeList())) {
constructionCompany.setProjectCode(StringUtils.join(constructionCompanyVo.getProjectCodeList(), ","));
constructionCompany.setProjectName(StringUtils.join(constructionCompanyVo.getProjectNameList(), ","));
}
```
这里使用了 Apache Commons Lang3 库中的 ArrayUtils 和 StringUtils 类。
阅读全文