Java两个List间并集与差集

今天下午在家办公遇到个问题,手动调用公式计算得分,需要对之前的得分进行修改操作,条件是根据多个字段,确实难搞哇,弄了一下午

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 设置综合得分返回数据
* @param scoreParam 综合得分信息
* @param evaluationType 评价类型(年度、季度)
* @param calculateScoreList 当前计算的得分数据
* @return 综合得分数据
*/
public List<SupplierComscore> comScoreUpsert(SupplierComscore scoreParam, String evaluationType, List<SupplierComscore> calculateScoreList) {

List<SupplierComscore> updateList = new ArrayList<>();

// 已入库的得分数据
List<SupplierComscore> scoreList = supplierComscoreService.selectComscoreList(scoreParam, evaluationType);

// 修改的数据
List<SupplierComscore> resultList = calculateScoreList.stream()
.filter(item -> scoreList.stream().map(e -> e.getSupplierCode() + "|" + e.getProductType())
.collect(Collectors.toList()).contains(item.getSupplierCode() + "|" + item.getProductType()))
.collect(Collectors.toList());

// 获取得分结果信息
if (CollectionUtils.isNotEmpty(resultList)) {
resultList.forEach(item -> {
scoreList.stream().filter(e -> e.getSupplierCode().equals(item.getSupplierCode()) && e.getProductType().equals(item.getProductType()))
.findFirst().ifPresent(s -> {
item.setId(s.getId());

// 判断综合评分是否修改
if (s.getBaseScore() != null) {
item.setBaseScore(item.getScore());
item.setScore(s.getScore());
}

// 判断指标得分是否修改
if (StrUtil.isNotBlank(s.getBaseIndexData())) {
item.setBaseIndexData(item.getIndexData());
item.setIndexData(s.getIndexData());
}

// 判断供应商评级是否修改
if (s.getBaseSupplierLevel() != null) {
item.setBaseSupplierLevel(item.getSupplierLevel());
item.setSecretLevel(s.getSecretLevel());
}

// 设置原先数据状态
item.setStatus(s.getStatus());
});
});
updateList = resultList.stream().filter(e ->
StrUtil.toString(e.getStatus()).equals(SalmKpiConstants.ASSESS_STATUS_EVALUATE_RELEASED) || StrUtil.toString(e.getStatus()).equals(SalmKpiConstants.ASSESS_STATUS_EVALUATE_REJECTED))
.collect(Collectors.toList());
}

// 新增的数据
List<SupplierComscore> otherList = calculateScoreList.stream().filter(item -> !resultList.contains(item)).collect(Collectors.toList());
return ListUtils.union(otherList, updateList);
}