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

基于列表中已有对象更新新对象并替换旧对象的实现问题

问题描述

我有一个SomeObject类型的对象列表,每个对象包含唯一的id字段,以及value1、value2、value3、value4这几个整数字段。当创建一个和列表中已有对象id相同的新对象时,我希望把旧对象的各个value属性值加到新对象对应的属性上,然后从列表中删除旧对象。

我试过把对象放进Set集合,但因为id相同但其他属性值不同,这个方案行不通。


当前代码

while (true) {
  String input = scanner.nextLine();
  int id;
  int value1;
  int value2;
  int value3;
  int value4;
  List<SomeObject> list = new ArrayList<>();
  SomeObject record = new SomeObject(id, value1, value2, value3, value4);
  list.add(record);

  for (SomeObject s : list) {
 
     if (s.getId() == id) {
        s.update1(value1);
        s.update2(value2);
        s.update3(value3);
        s.update4(value4);
        break;          
     }       
   }
  list.removeIf(n -> n.getId() == id);
}

SomeObject类代码

public class SomeObject {
    private int id;
    private int value1;
    private int value2;
    private int value3;
    private int value4;
    
    // getters, constructor
    
    public int update1(int x) {
        return this.value1 = value1 + x;
    }
    
    public int update2(int x) {
        return this.value2 = value2 + x;
    }
    
    public int update3(int x) {
        return this.value3 = value3 + x;
    }
    
    public int update4(int x) {
        return this.value4 = value4 + x;
    }
}

输入输出示例

输入示例

id=1243245 1 1 1 1 // 旧对象
id=1243245 2 2 2 2 // 新对象

当前输出

id=1243245 2 2 2 2 // <- 新对象的数值

期望输出

id=1243245 3 3 3 3 // <- 合并后的数值

解决方案

你的代码存在两个核心问题:

  1. 列表每次循环都重新创建:List<SomeObject> list = new ArrayList<>();放在while循环内部,导致每次输入都生成全新列表,之前的旧对象完全没被保留,根本没法执行合并逻辑。
  2. 逻辑顺序完全错误:先把新对象加入列表,再遍历找相同id的对象(此时列表里只有刚加的新对象),随后又把这个对象删掉,完全没处理旧对象的合并需求。

修正方案(基于List)

把列表移到循环外部持久化存储,先检查旧对象再处理合并:

// 列表定义在循环外,保存所有对象
List<SomeObject> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

while (true) {
    String input = scanner.nextLine();
    // 解析输入(按"id=xxx v1 v2 v3 v4"格式处理)
    String[] parts = input.split(" ");
    int id = Integer.parseInt(parts[0].substring(3));
    int value1 = Integer.parseInt(parts[1]);
    int value2 = Integer.parseInt(parts[2]);
    int value3 = Integer.parseInt(parts[3]);
    int value4 = Integer.parseInt(parts[4]);

    SomeObject newRecord = new SomeObject(id, value1, value2, value3, value4);
    // 查找列表中是否存在相同id的旧对象
    SomeObject oldRecord = null;
    for (SomeObject s : list) {
        if (s.getId() == id) {
            oldRecord = s;
            break;
        }
    }

    if (oldRecord != null) {
        // 将旧对象的value值合并到新对象上
        newRecord.update1(oldRecord.getValue1());
        newRecord.update2(oldRecord.getValue2());
        newRecord.update3(oldRecord.getValue3());
        newRecord.update4(oldRecord.getValue4());
        // 删除旧对象
        list.remove(oldRecord);
    }
    // 添加合并后的新对象
    list.add(newRecord);

    // 打印当前结果验证
    for (SomeObject obj : list) {
        System.out.printf("id=%d %d %d %d %d%n", obj.getId(), obj.getValue1(), obj.getValue2(), obj.getValue3(), obj.getValue4());
    }
}

更高效方案(基于Map)

用Map以id为键存储对象,查找和删除操作时间复杂度为O(1),比遍历列表更高效:

Map<Integer, SomeObject> objectMap = new HashMap<>();
Scanner scanner = new Scanner(System.in);

while (true) {
    String input = scanner.nextLine();
    String[] parts = input.split(" ");
    int id = Integer.parseInt(parts[0].substring(3));
    int value1 = Integer.parseInt(parts[1]);
    int value2 = Integer.parseInt(parts[2]);
    int value3 = Integer.parseInt(parts[3]);
    int value4 = Integer.parseInt(parts[4]);

    SomeObject existingObj = objectMap.get(id);
    if (existingObj != null) {
        // 合并值:新值 = 输入值 + 旧对象值
        value1 += existingObj.getValue1();
        value2 += existingObj.getValue2();
        value3 += existingObj.getValue3();
        value4 += existingObj.getValue4();
        // 移除旧对象
        objectMap.remove(id);
    }
    // 放入合并后的对象
    objectMap.put(id, new SomeObject(id, value1, value2, value3, value4));

    // 打印结果
    for (Map.Entry<Integer, SomeObject> entry : objectMap.entrySet()) {
        SomeObject obj = entry.getValue();
        System.out.printf("id=%d %d %d %d %d%n", obj.getId(), obj.getValue1(), obj.getValue2(), obj.getValue3(), obj.getValue4());
    }
}

补充说明

  • 需要给SomeObject类补充getValue1()等getter方法,以及接收id和四个value参数的构造函数。
  • 输入解析逻辑可根据实际输入格式调整,上述代码默认输入为id=xxx v1 v2 v3 v4格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 05:15:40