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

Java Stream处理对象列表:与前项相同属性设为null

问题:对去重排序后的SystemAudit列表进行属性差异处理

原始数据

我们有一个从数据库获取的、基于Hibernate Envers的SystemAudit对象列表:

List<SystemAudit> systemAuditList = new ArrayList<>();

systemAuditList.add(new SystemAudit(0, "A0", "B0", "C0"));
systemAuditList.add(new SystemAudit(1, "A0", "B1", "C0"));
systemAuditList.add(new SystemAudit(2, "A0", "B1", "C0"));
systemAuditList.add(new SystemAudit(3, "A1", "B1", "C0"));
systemAuditList.add(new SystemAudit(4, "A1", "B2", "C0"));
systemAuditList.add(new SystemAudit(5, "A1", "B2", "C0"));
systemAuditList.add(new SystemAudit(6, "A2", "B2", "C0"));
systemAuditList.add(new SystemAudit(7, "A2", "B3", "C1"));
systemAuditList.add(new SystemAudit(8, "A2", "B3", "C1"));

现有处理逻辑

已通过以下Stream代码对列表按指定属性去重并排序:

var systemAuditListCorrectlyFiltered = SystemAudit.createRandomList().stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator
                        .comparing(SystemAudit::getDescriptionFieldA)
                        .thenComparing(SystemAudit::getDescriptionFieldB)
                        .thenComparing(SystemAudit::getDescriptionFieldC))),
                ArrayList::new))
        .stream()
        .map(SystemAuditHelper::create)
        .sorted(Comparator.comparing(SystemAuditHelper::getId, Comparator.nullsLast(Comparator.reverseOrder())))
        .collect(Collectors.toList());

当前输出结果

执行上述代码后得到的结果:

[{7, A2, B3, C1},
 {6, A2, B2, C0},
 {4, A1, B2, C0},
 {3, A1, B1, C0},
 {1, A0, B1, C0},
 {0, A0, B0, C0}]

期望输出结果

我们需要将列表中每个对象与后一个对象对比,相同属性设为null,仅保留不同的属性,期望结果如下:

[{7, null, B3,   C1  },
 {6, A2,   null, null},
 {4, null, B2,   null},
 {3, A1,   null, null},
 {1, null, B1,   null},
 {0, A0,   B0,   C0  }]

实现方案

要实现该需求,只需在已排序去重的列表基础上,遍历每个元素并与下一个元素对比属性,将相同属性置为null,最后一个元素保留所有属性。具体代码如下:

List<SystemAuditHelper> finalResult = new ArrayList<>();
int listSize = systemAuditListCorrectlyFiltered.size();

for (int i = 0; i < listSize; i++) {
    SystemAuditHelper current = systemAuditListCorrectlyFiltered.get(i);
    if (i == listSize - 1) {
        // 最后一个元素保留所有属性
        finalResult.add(current);
    } else {
        SystemAuditHelper next = systemAuditListCorrectlyFiltered.get(i + 1);
        // 对比每个属性,相同则设为null
        String fieldA = current.getDescriptionFieldA().equals(next.getDescriptionFieldA()) ? null : current.getDescriptionFieldA();
        String fieldB = current.getDescriptionFieldB().equals(next.getDescriptionFieldB()) ? null : current.getDescriptionFieldB();
        String fieldC = current.getDescriptionFieldC().equals(next.getDescriptionFieldC()) ? null : current.getDescriptionFieldC();
        // 创建新的SystemAuditHelper对象加入结果列表
        finalResult.add(new SystemAuditHelper(current.getId(), fieldA, fieldB, fieldC));
    }
}

说明

  • 遍历过程中,对除最后一个元素外的每个对象,逐一对比其与下一个对象的descriptionFieldA、descriptionFieldB、descriptionFieldC属性;
  • 若属性值相同则设为null,不同则保留原属性值;
  • 最后一个元素无后续元素,直接保留所有属性值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 18:24:53