优化后的代码:

List<RegionInfoDto> regionInfoDtos = tempLocalCache.get(tempId.toString());
if (regionInfoDtos != null && !regionInfoDtos.isEmpty() && regionInfoDtos.get(0).getIsExist()) {
    return regionInfoDtos;
}
YxShippingTemplates yxShippingTemplates = yxShippingTemplatesMapper.selectById(tempId);
if (yxShippingTemplates.getUndeliverableInfo() == null) {
    RegionInfoDto regionInfo = new RegionInfoDto();
    regionInfo.setIsExist(false);
    regionInfoDtos = Collections.singletonList(regionInfo);
} else {
    regionInfoDtos = JSONObject.parseArray(yxShippingTemplates.getUndeliverableInfo(), RegionInfoDto.class);
}
tempLocalCache.setLocalCache(tempId.toString(), regionInfoDtos);
return regionInfoDtos;

优化说明:

  1. 使用 isEmpty() 方法代替 CollectionUtil.isNotEmpty() 方法,可以减少对第三方工具类的依赖。
  2. 判断 List 是否为空时,可以使用 != null 代替 !CollectionUtil.isEmpty(),可以提高代码可读性。
  3. 将判断条件中的 equals(false) 简化为直接调用 getIsExist() 方法,使代码更加简洁。
  4. 在 List 为空时,可以使用 Collections.singletonList() 方法来创建只包含一个元素的 List,可以提高代码效率。
  5. 在解析 JSON 字符串时,使用 JSONObject.parseArray() 方法代替 JSONArray.parseArray() 方法,可以减少对第三方工具类的依赖。
优化 Java 代码:获取 RegionInfoDto 列表

原文地址: https://www.cveoy.top/t/topic/ovmm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录