优化后的代码如下:

List scoreRecordList = new ArrayList<>(); if (activityScoreItemVO != null && activityScoreItemVO.getScoreItemTable() != null && activityScoreItemVO.getScoreItemTable().getTableSource() != null) { for (ScoreItemTableSourceVO scoreItemTableSourceVO : activityScoreItemVO.getScoreItemTable().getTableSource()) { if (CollectionUtils.isNotEmpty(scoreItemTableSourceVO.getScoreList())) { for (ScoreItemVO scoreItem : scoreItemTableSourceVO.getScoreList()) { ScoreRecord scoreRecord = createScoreRecode(scoreItem, activityId); if (scoreRecord != null) { scoreRecordList.add(scoreRecord); } } } } } return scoreRecordList;

这样做的好处是:

  1. 可读性更高,易于理解和维护。

  2. 避免了过度使用Lambda表达式和链式调用,提高了代码的可读性和可维护性。

  3. 减少了方法调用的次数和对象的创建,从而提高了性能。

Java 代码优化:使用传统循环替代 Optional 和 Lambda 表达式

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

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