You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Stream分组CSV重复记录时按中心分配可重置的唯一组号

客户重复记录分组分配组号实现

基础说明

现有代码已实现CSV读取、重复客户筛选、按center分组功能,需新增按center内相同客户分配递增组号能力,切换center时组号重置为1。

第一步:修改Client实体类

新增group字段,补全对应get/set方法,修改toString输出group信息:

package entities;

public class Client {
    private String id;
    private String name;
    private String mother;
    private String birth;
    private String center;
    // 新增组号字段
    private int group;

    public Client() {
    }

    public Client(String id, String name, String mother, String birth, String center) {
        this.id = id;
        this.name = name;
        this.mother = mother;
        this.birth = birth;
        this.center = center;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMother() {
        return mother;
    }

    public void setMother(String mother) {
        this.mother = mother;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getCenter() {
        return center;
    }

    public void setCenter(String center) {
        this.center = center;
    }

    @Override
    public String toString() {
        return "Client [group=" + group + ", id=" + id + ", name=" + name + ", mother=" + mother + ", birth=" + birth + ", center=" + center
                + "]";
    }
}

第二步:修改主程序逻辑

修正原有代码中isDuplicate方法的位置错误,新增center内组号分配逻辑:

package application;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import entities.Client;

public class Program {
    public static void main(String[] args) throws IOException {
        Pattern pattern = Pattern.compile(",");

        List<Client> file = Files.lines(Paths.get("src/Client.csv"))
                .skip(1)
                .map(line -> {
                    String[] fields = pattern.split(line);
                    return new Client(fields[0], fields[1], fields[2], fields[3], fields[4]);
                })
                .collect(Collectors.toList());

        // 过滤出存在重复的客户记录
        List<Client> duplicateClients = file
                .stream()
                .filter(x -> file.stream().anyMatch(y -> isDuplicate(x, y)))
                .collect(Collectors.toList());

        // 按center分组,保留center的出现顺序
        Map<String, List<Client>> groupedByCenter = duplicateClients
                .stream()
                .collect(Collectors.groupingBy(Client::getCenter, LinkedHashMap::new, Collectors.toList()));

        // 为每个center内的相同客户分配组号
        groupedByCenter.forEach((center, clients) -> {
            // 同一center下按姓名+母亲+生日二次分组,保留客户出现顺序
            Map<String, List<Client>> sameClientGroups = clients.stream()
                    .collect(Collectors.groupingBy(
                            c -> c.getName() + "|" + c.getMother() + "|" + c.getBirth(),
                            LinkedHashMap::new,
                            Collectors.toList()
                    ));
            // 组号从1开始递增
            int groupNo = 1;
            for (List<Client> clientGroup : sameClientGroups.values()) {
                for (Client client : clientGroup) {
                    client.setGroup(groupNo);
                }
                groupNo++;
            }
        });

        // 输出最终结果
        groupedByCenter.entrySet().forEach(System.out::println);
    }

    private static Boolean isDuplicate(Client x, Client y) {
        return !x.getId().equals(y.getId())
                && x.getName().equals(y.getName())
                && x.getMother().equals(y.getMother())
                && x.getBirth().equals(y.getBirth());
    }
}

输出效果

完全符合需求规则:

1=[Client [group=1, id=1, name=Antonio Carlos da Silva, mother=Ana da Silva, birth= 2008/03/31, center=1], Client [group=1, id=7, name=Antonio Carlos da Silva, mother=Ana da Silva, birth= 2008/03/31, center=1]]
2=[Client [group=1, id=3, name=Pedro de Albuquerque, mother=Maria de Albuquerque, birth=2006/04/03, center=2], Client [group=1, id=6, name=Pedro de Albuquerque, mother=Maria de Albuquerque, birth=2006/04/03, center=2], Client [group=1, id=11, name=Pedro de Albuquerque, mother=Maria de Albuquerque, birth=2006/04/03, center=2]]
4=[Client [group=1, id=5, name=Ralfo dos Santos Filho, mother=Helena dos Santos, birth=2012/02/21, center=4], Client [group=1, id=8, name=Ralfo dos Santos Filho, mother=Helena dos Santos, birth=2012/02/21, center=4], Client [group=1, id=12, name=Ralfo dos Santos Filho, mother=Helena dos Santos, birth=2012/02/21, center=4]]

内容的提问来源于stack exchange,提问作者Adalberto J. Brasaca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 06:06:04