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

按Section和Category分组List<Statement>,转换为多层嵌套集合结构

实现嵌套分组的解决方案

这事儿用Java 8+的Stream API就能轻松搞定,步骤很清晰,我给你拆解一下:

首先得先把需要的嵌套实体类定义好,毕竟要把分组结果装进对应的结构里:

1. 定义实体类

// 原始的Statement类(假设它包含section和category属性)
public class Statement {
    private String section;
    private String category;
    // 其他业务属性...
    
    // 构造器、getter和setter方法,根据实际需求补充
    public String getSection() { return section; }
    public String getCategory() { return category; }
}

// 第二层的CategoryCollection,封装分类和对应的Statement列表
public class CategoryCollection {
    private String category;
    private List<Statement> statements;

    public CategoryCollection(String category, List<Statement> statements) {
        this.category = category;
        this.statements = statements;
    }

    // getter和setter按需添加
}

// 最外层的SectionCollection,封装章节和对应的分类集合
public class SectionCollection {
    private String section;
    private List<CategoryCollection> categoryCollections;

    public SectionCollection(String section, List<CategoryCollection> categoryCollections) {
        this.section = section;
        this.categoryCollections = categoryCollections;
    }

    // getter和setter按需添加
}

2. 核心分组逻辑

用Stream的groupingBy先按Section做第一层分组,再在每个分组内按Category做第二层分组,最后把分组结果转换成你需要的嵌套结构:

// 假设这是你的原始Statement列表
List<Statement> originalStatements = new ArrayList<>();
// 往originalStatements里填充数据...

// 执行分组转换
List<SectionCollection> finalResult = originalStatements.stream()
    // 第一步:按section分组,得到{section: List<Statement>}的Map
    .collect(Collectors.groupingBy(Statement::getSection))
    .entrySet()
    .stream()
    // 把每个section的分组转换成SectionCollection对象
    .map(sectionEntry -> {
        String sectionName = sectionEntry.getKey();
        List<Statement> statementsInSection = sectionEntry.getValue();
        
        // 第二步:在当前section内,按category分组并转换成CategoryCollection列表
        List<CategoryCollection> categoryGroups = statementsInSection.stream()
            .collect(Collectors.groupingBy(Statement::getCategory))
            .entrySet()
            .stream()
            .map(categoryEntry -> new CategoryCollection(
                categoryEntry.getKey(),
                categoryEntry.getValue()
            ))
            .collect(Collectors.toList());
        
        return new SectionCollection(sectionName, categoryGroups);
    })
    .collect(Collectors.toList());

可选:添加排序逻辑

如果需要按Section或Category的名称排序,只需要在对应的stream链里加入sorted即可。比如按Section名称升序排序,同时给Category也排序:

List<SectionCollection> sortedResult = originalStatements.stream()
    .collect(Collectors.groupingBy(Statement::getSection))
    .entrySet()
    .stream()
    // 按section名称升序排序
    .sorted(Map.Entry.comparingByKey())
    .map(sectionEntry -> {
        String sectionName = sectionEntry.getKey();
        List<Statement> statementsInSection = sectionEntry.getValue();
        
        List<CategoryCollection> sortedCategoryGroups = statementsInSection.stream()
            .collect(Collectors.groupingBy(Statement::getCategory))
            .entrySet()
            .stream()
            // 按category名称升序排序
            .sorted(Map.Entry.comparingByKey())
            .map(categoryEntry -> new CategoryCollection(
                categoryEntry.getKey(),
                categoryEntry.getValue()
            ))
            .collect(Collectors.toList());
        
        return new SectionCollection(sectionName, sortedCategoryGroups);
    })
    .collect(Collectors.toList());

这样就能完美得到你需要的多层嵌套结构啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:20:26