2种思路:利用treeset或者map均可实现去重
解决方案:
1、treeset去重:
//先根据对象某属性进行排序,下面示例为保留最新的一条数据
toDoList.sort(Comparator.comparing(ActTaskDto::getProcessNum).reversed());
//利用treeset实现去重
actTaskDtosNew = toDoList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<ActTaskDto>(Comparator.comparing(ActTaskDto::getProcessNum))), ArrayList::new)
);
上述代码中toDolist和actTaskDtosNew 都是对象list。
2、map实现去重
map实现去重的思路:将去重条件作为map中的key
Map<String, Integer> map = new HashMap<>();
for(SupplyPaymentPolicyManagementBasicDto s : dataList) {
if (map.containsKey(s.getSupplyCode()+ s.getAddressId())) {
Integer num = map.get(s.getSupplyCode()+ s.getAddressId());
map.put(s.getSupplyCode()+ s.getAddressId(), num + 1);
//return true;
} else {
map.put(s.getSupplyCode()+ s.getAddressId(), 1);
}
}
经常用上面代码用于校验是否有重复数据。