合并两个列表,并根据id去重,保留basicList中的实体:

// 合并两个列表,并根据id去重,保留list1中的实体
        List<BasPlaceAddressCategory> mergedList = Stream.concat(basicList.stream(), list.stream())
                .collect(Collectors.toMap(
                        BasPlaceAddressCategory::getId, // 根据id作为键
                        entity -> entity, // 值就是实体本身
                        (existing, replacement) -> existing // 如果有重复的id,保留现有的实体
                ))
                .values()
                .stream()
                .collect(Collectors.toList());

如果你还想改list1的值为list2中的某个值则:

// 合并两个列表,并根据id去重,保留basicList中的实体
            List<DeptReportDto> mergedList = Stream.concat(slListResult.stream(), newReportList.stream())
                    .collect(Collectors.toMap(
                            DeptReportDto::getOfficeId, // 根据id作为键
                            entity -> entity, // 值就是实体本身
                            (existing, replacement) -> {
                // 在这里修改 existing 的属性,例如:
                existing.setSlCount(replacement.getSlCount());
                return existing; // 返回修改后的 existing
            }
                    ))
                    .values()
                    .stream()
                    .collect(Collectors.toList());
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐