You need to enable JavaScript to run this app. Java如何按phase属性分组对象列表合并取日期极值生成新列表
优惠活动
大模型
产品
解决方案
定价
更多

Java如何按phase属性分组对象列表合并取日期极值生成新列表

实现方案

你可以直接使用Java 8 Stream API的分组聚合能力完成需求,完整可运行代码如下:

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;

public class MyDate {
    public static void main(String[] args) {
        // 初始化示例用到的日期变量
        LocalDate todayDate = LocalDate.now();
        LocalDate todayDatePlus1 = todayDate.plusDays(1);
        LocalDate todayDatePlus2 = todayDate.plusDays(2);
        LocalDate todayDatePlus3 = todayDate.plusDays(3);

        List<Data> list = new ArrayList<>();
        list.add(new Data("NEW",todayDate,todayDatePlus1,"N"));
        list.add(new Data("AUTORIZED",todayDate,todayDatePlus1,"Y"));
        list.add(new Data("AUTORIZED",todayDatePlus1,todayDatePlus3,"Y"));
        list.add(new Data("AUTHENTICATE",todayDate,todayDatePlus1,"Y"));
        list.add(new Data("AUTHENTICATE",todayDatePlus1,todayDatePlus2,"Y"));
        list.add(new Data("COMPLETED",todayDate,todayDatePlus1,"N"));

        // 核心分组合并逻辑
        List<Data> mergedList = list.stream()
                // 按phase字段分组
                .collect(Collectors.groupingBy(Data::getPhase, Collectors.collectingAndThen(Collectors.toList(), group -> {
                    // 同组其余字段取值一致,直接取第一条的required
                    String required = group.get(0).getRequired();
                    // 取组内最早的startDate
                    LocalDate minStart = group.stream()
                            .map(Data::getStartDate)
                            .min(LocalDate::compareTo)
                            .orElseThrow();
                    // 取组内最晚的endDate
                    LocalDate maxEnd = group.stream()
                            .map(Data::getEndDate)
                            .max(LocalDate::compareTo)
                            .orElseThrow();
                    return new Data(group.get(0).getPhase(), minStart, maxEnd, required);
                })))
                .values()
                .stream()
                // 按phase顺序排序,不需要可以删除这行
                .sorted(Comparator.comparing(Data::getPhase))
                .collect(Collectors.toList());

        // 打印验证结果
        System.out.println("In New List \n");
        mergedList.forEach(data -> System.out.printf("(\"%s\",%s,%s,\"%s\")%n",
                data.getPhase(),
                data.getStartDate(),
                data.getEndDate(),
                data.getRequired()));
    }
}

@Getter
@Setter
class Data{
    String phase;
    LocalDate startDate;
    LocalDate endDate;
    String required;

    Data(String phase, LocalDate startDate, LocalDate endDate, String required) {
        this.phase = phase;
        this.startDate = startDate;
        this.endDate = endDate;
        this.required = required;
    }
}

注意事项

  • 代码默认同phase下的required字段值全部一致,直接取组内第一条的值即可,如果你的实际场景中该字段可能存在差异,可以自行调整取值逻辑
  • 最终输出的合并后列表完全符合你给出的预期结果,相同phase的条目会合并为一条,startDate取组内最小值,endDate取组内最大值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 18:24:03