现在有两个不同对象的集合合并成一个新的VillagePartyOrgVO集合memCode不为空时根据memCode分组否则根据姓名和身份证进行分组数据进行分组时根据集合对象中的type来区分当type不同时对重复数据进行标记并把有标记的重复数据的职位处理成list集合然后把最后的结果list转成page对象java8 steam 应该怎么写
这个问题比较复杂,需要多次操作和组合,可以参考以下代码:
List<VillagePartyOrgVO> list1 = new ArrayList<>(); // 第一个集合
List<VillagePartyOrgVO> list2 = new ArrayList<>(); // 第二个集合
// 合并两个集合
List<VillagePartyOrgVO> mergedList = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
// 根据memCode或姓名+身份证分组
Map<String, List<VillagePartyOrgVO>> groupedMap = mergedList.stream()
.collect(Collectors.groupingBy(vo -> StringUtils.isNotBlank(vo.getMemCode()) ? vo.getMemCode() : vo.getName() + vo.getIdCard()));
// 对重复数据进行标记,并把有标记的重复数据的职位处理成list集合
groupedMap.values().forEach(list -> {
Map<String, List<VillagePartyOrgVO>> typeGroupMap = list.stream().collect(Collectors.groupingBy(VillagePartyOrgVO::getType));
typeGroupMap.values().forEach(typeGroupList -> {
if (typeGroupList.size() > 1) {
typeGroupList.forEach(vo -> vo.setDuplicate(true));
List<String> positionList = typeGroupList.stream().map(VillagePartyOrgVO::getPosition).collect(Collectors.toList());
typeGroupList.forEach(vo -> vo.setPositionList(positionList));
}
});
});
// 把最后的结果list转成page对象
Page<VillagePartyOrgVO> page = new PageImpl<>(mergedList, PageRequest.of(0, mergedList.size()), mergedList.size());
``
原文地址: https://www.cveoy.top/t/topic/cnI5 著作权归作者所有。请勿转载和采集!