public static String getDictLabel(String dictType, String dictValue, String separator) { StringBuilder propertyString = new StringBuilder(); List<SysDictData> datas = getDictCache(dictType); if (StringUtils.isNotNull(datas)) { if (containsAny(separator, dictValue)) { for (SysDictData dict : datas) { for (String value : dictValue.split(separator)) { if (value.equals(dict.getDictValue())) { propertyString.append(dict.getDictLabel()).append(separator); break; } } } } else { for (SysDictData dict : datas) { if (dictValue.equals(dict.getDictValue())) { return dict.getDictLabel(); } } } } return stripEnd(propertyString.toString(), separator); }优化这段代码
时间: 2024-03-26 07:41:01 浏览: 19
首先,可以考虑将 `getDictCache(dictType)` 方法的调用移到 if 语句外面,以避免重复调用。同时,可以使用增强的 for 循环来遍历数据,使代码更加简洁。最后,可以使用 `StringUtils.join()` 方法来代替手动拼接字符串。
改进后的代码如下:
```
public static String getDictLabel(String dictType, String dictValue, String separator) {
List<SysDictData> datas = getDictCache(dictType);
if (StringUtils.isNull(datas)) {
return "";
}
StringBuilder propertyString = new StringBuilder();
if (containsAny(separator, dictValue)) {
for (SysDictData dict : datas) {
for (String value : dictValue.split(separator)) {
if (value.equals(dict.getDictValue())) {
propertyString.append(dict.getDictLabel()).append(separator);
break;
}
}
}
} else {
for (SysDictData dict : datas) {
if (dictValue.equals(dict.getDictValue())) {
return dict.getDictLabel();
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)