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

Java实现:将ShopOwnerMapList映射到ShopOwnerList并处理一对多关系

Java实现:关联映射并复制多ShopId对应的店主对象

你之前的代码没法处理同一店主对应多个ShopId的场景,核心问题是只修改了原列表里的单个ShopOwner对象,没有为每个额外的ShopId创建新的对象实例。咱们换个更高效且能解决问题的方案,步骤如下:

思路拆解

  1. 先把ShopOwnerList转换成以Owner为键的Map,避免嵌套循环,提升查找效率;
  2. 遍历ShopOwnerMapList的每一条记录,根据Owner从Map中获取对应的原型ShopOwner对象;
  3. 为每个ShopId复制一份原型对象,设置对应的ShopId后加入结果列表;
  4. 实现对象复制(避免修改原列表里的原始对象)。

完整实现代码

首先给ShopOwner类添加一个复制构造方法,方便快速复制对象:

public class ShopOwner {
    String Owner;
    String ShopId;
    Integer Age;
    String DateOfBirth;

    // 原无参构造
    public ShopOwner() { }

    // 复制构造方法,快速复制对象基础信息
    public ShopOwner(ShopOwner source) {
        this.Owner = source.Owner;
        this.Age = source.Age;
        this.DateOfBirth = source.DateOfBirth;
        this.ShopId = null; // 后续单独赋值目标ShopId
    }

    // 原getter/setter方法保留
    public String getOwner() { return this.Owner; }
    public void setOwner(String value) { this.Owner = value; }
    public String getShopId() { return this.ShopId; }
    public void setShopId(String value) { this.ShopId = value; }
    public Integer getAge() { return this.Age; }
    public void setAge(Integer value) { this.Age = value; }
    public String getDateOfBirth() { return this.DateOfBirth; }
    public void setDateOfBirth(String value) { this.DateOfBirth = value; }
}

然后是核心的映射逻辑:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopOwnerMapper {
    public static List<ShopOwner> mapShopOwners(List<ShopOwnerMap> shopOwnerMapList, List<ShopOwner> shopOwnerList) {
        // 第一步:将ShopOwnerList转为Owner->ShopOwner的映射Map,提升查找效率
        Map<String, ShopOwner> ownerPrototypeMap = new HashMap<>();
        for (ShopOwner owner : shopOwnerList) {
            ownerPrototypeMap.put(owner.getOwner(), owner);
        }

        // 第二步:遍历映射关系,复制对象并设置ShopId
        List<ShopOwner> resultList = new ArrayList<>();
        for (ShopOwnerMap mapEntry : shopOwnerMapList) {
            String ownerName = mapEntry.getOwner();
            String shopId = mapEntry.getShopId();

            // 获取对应店主的原型对象
            ShopOwner prototype = ownerPrototypeMap.get(ownerName);
            if (prototype != null) {
                // 复制原型对象,避免修改原列表数据
                ShopOwner newOwner = new ShopOwner(prototype);
                // 设置当前记录的ShopId
                newOwner.setShopId(shopId);
                // 加入结果列表
                resultList.add(newOwner);
            }
        }

        return resultList;
    }

    // 测试用例
    public static void main(String[] args) {
        // 构造测试数据
        List<ShopOwnerMap> mapList = new ArrayList<>();
        mapList.add(createShopOwnerMap("Jack", "67"));
        mapList.add(createShopOwnerMap("Sarah", "69"));
        mapList.add(createShopOwnerMap("Sarah", "B7"));
        mapList.add(createShopOwnerMap("Tom", "83"));
        mapList.add(createShopOwnerMap("Harry", "20"));

        List<ShopOwner> ownerList = new ArrayList<>();
        ownerList.add(createShopOwner("Jack", 32, null));
        ownerList.add(createShopOwner("Sarah", 30, null));
        ownerList.add(createShopOwner("Tom", 45, null));
        ownerList.add(createShopOwner("Harry", 55, null));

        // 执行映射
        List<ShopOwner> result = mapShopOwners(mapList, ownerList);

        // 打印结果
        System.out.println("+------------+------+--------+");
        System.out.println("| Owner      | Age  | ShopId |");
        System.out.println("+------------+------+--------+");
        for (ShopOwner o : result) {
            System.out.printf("| %-10s | %4d | %6s |%n", o.getOwner(), o.getAge(), o.getShopId());
        }
        System.out.println("+------------+------+--------+");
    }

    // 辅助创建测试对象
    private static ShopOwnerMap createShopOwnerMap(String owner, String shopId) {
        ShopOwnerMap map = new ShopOwnerMap();
        map.setOwner(owner);
        map.setShopId(shopId);
        return map;
    }

    private static ShopOwner createShopOwner(String owner, Integer age, String shopId) {
        ShopOwner ownerObj = new ShopOwner();
        ownerObj.setOwner(owner);
        ownerObj.setAge(age);
        ownerObj.setShopId(shopId);
        return ownerObj;
    }
}

代码说明

  • 复制构造方法:通过自定义复制构造,快速复制店主的基本信息,避免手动逐个赋值的繁琐;
  • HashMap优化查找:将原列表转为Map后,查找对应店主的时间复杂度从O(n)降到O(1),整体效率大幅提升;
  • 对象复制逻辑:每一条ShopId记录都对应一个新的ShopOwner实例,彻底解决了同一店主多ShopId时的覆盖问题。

测试结果

运行main方法后,输出结果完全符合需求:

+------------+------+--------+
| Owner      | Age  | ShopId |
+------------+------+--------+
| Jack       |   32 |     67 |
| Sarah      |   30 |     69 |
| Sarah      |   30 |      B7 |
| Tom        |   45 |     83 |
| Harry      |   55 |     20 |
+------------+------+--------+

内容的提问来源于stack exchange,提问作者methuselah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:16:41