使用 Set 过滤评分项数据

Set<String> excludeAssessRelationIdsSet = excludeAssessRelationIds.stream()
            .collect(Collectors.toSet());

return CollectionUtils.emptyIfNull(scoreItemTableSourceVOS)
        .stream()
        .filter(s -> !excludeAssessRelationIdsSet.contains(s.getExaminerEvaluationId()))
        .collect(Collectors.groupingBy(ScoreItemTableSourceVO::getAssessee));

代码解释:

  1. 使用 stream() 方法将 excludeAssessRelationIds 列表转换成 Stream 对象。
  2. 使用 collect(Collectors.toSet()) 方法将 Stream 对象转换为 Set 对象,并去重。
  3. 使用 filter() 方法过滤评分项数据,只保留 excludeAssessRelationIdsSet 中不存在的评分项。
  4. 使用 collect(Collectors.groupingBy()) 方法将符合条件的评分项数据按照 Assessee 进行分组。

优点:

  • 使用 Set 可以有效避免重复的评分项数据,提高代码效率。
  • 代码简洁易懂,可读性强。

注意事项:

  • CollectionUtils.emptyIfNull() 方法可以将 null 值转换为空集合,防止代码出现 NullPointerException。
  • ScoreItemTableSourceVO 应该包含 getExaminerEvaluationId()getAssessee() 方法。

示例:

假设 excludeAssessRelationIds 列表包含以下数据:

[1, 2, 3, 1, 4]

经过过滤后,excludeAssessRelationIdsSet 将包含以下数据:

[1, 2, 3, 4]

最终返回的结果将是按照 Assessee 分组的评分项数据,并且不包含 excludeAssessRelationIdsSet 中的评分项。


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

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