假设对象类型为VillagePartyOrgVO,包含属性memCode、姓名name、身份证号idCard、职位position、时间time、类型type。

首先需要根据memCode或姓名和身份证进行分组,可以使用groupingBy方法来实现:

Map<String, List<VillagePartyOrgVO>> groupMap = list.stream()
    .collect(Collectors.groupingBy(vo -> StringUtils.isNotBlank(vo.getMemCode()) ? vo.getMemCode() : vo.getName() + vo.getIdCard()));

接着需要对重复数据进行标记,并把职位拼接成逗号分隔的字符串。可以使用toMap方法来实现:

List<VillagePartyOrgVO> resultList = groupMap.values().stream()
    .map(subList -> {
        if (subList.size() == 1) {
            return subList.get(0);
        } else {
            VillagePartyOrgVO first = subList.get(0);
            String positions = subList.stream().map(VillagePartyOrgVO::getPosition).collect(Collectors.joining(","));
            first.setPosition(positions);
            first.setType("重复");
            return first;
        }
    })
    .sorted(Comparator.comparing(VillagePartyOrgVO::getTime))
    .collect(Collectors.toList());

最后需要把结果list转成page对象,可以使用subList方法来实现:

int pageSize = 10;
int total = resultList.size();
int pageCount = (int) Math.ceil((double) total / pageSize);
int pageNum = 1;
List<VillagePartyOrgVO> pageList = resultList.subList((pageNum - 1) * pageSize, Math.min(pageNum * pageSize, total));
Page<VillagePartyOrgVO> page = new PageImpl<>(pageList, PageRequest.of(pageNum - 1, pageSize), total);
``

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

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