怎么优化这段代码 String visits = 1; if CollectionUtilsisNotEmptydict visits = dictget0getDictValue; String finalVisits = visits; final ListServiceTimesAccountVo monthA
可以优化这段代码如下:
String visits = CollectionUtils.isNotEmpty(dict) ? dict.get(0).getDictValue() : "1";
List<ServiceTimesAccountVo> monthAccountVos = CollectionUtils.emptyIfNull(endOfMonthAccountVos).stream()
.filter(e -> e.getFrequency() >= NumberUtils.toInt(visits))
.collect(Collectors.toList());
优化的地方包括:
- 合并了visits的赋值逻辑,用三元运算符代替了if-else语句。
- 去掉了finalVisits的定义,直接使用visits。
- 删除了没必要的空格和换行,使代码更加紧凑。
原文地址: https://www.cveoy.top/t/topic/iiHE 著作权归作者所有。请勿转载和采集!