Java根据分组key构建合并数据集的代码详解

 更新时间:2025年06月27日 09:02:22   作者:南国以南i  
这篇文章主要介绍了Java中如何对List数据进行双层分组合并,通过sub_product_name和approval_result作为分组键,使用rowspan实现跨行合并,最终生成前端可展示的结构化数据集,需要的朋友可以参考下

背景

Java 需要返回一组数据供前端展示,获取到的数据格式如下:
List<Map<String, Object>> varSummary 存在多组数据,varSummary中map结构一致

[{sub_product_name=生活费-生意贷, approval_result=其它, marital_state=3},
 {sub_product_name=生活费-生意贷, approval_result=其它, marital_state=3}, 
 {sub_product_name=生活费-生意贷, approval_result=通过, marital_state=3},
 {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3}, 
 {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3}, 
 {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3}, 
 {sub_product_name=生活费-联合贷, approval_result=其他, marital_state=3}]

需求一:

已知需要合并分组的属性有:sub_product_name、approval_result、varSummary中的数据,需要构建合并对象属性

核心逻辑说明:

  1. 双层分组处理:

外层分组:按 sub_product_name 分组(示例中3条数据均相同)

内层分组:在外层分组内按 approval_result 分组(示例中前两条"其它"相同,第三条"通过"不同)

  1. 合并规则:

第一列(col=0):合并相同 sub_product_name 的行(rowspan=3)

第二列(col=1):合并相同 approval_result 的连续行(rowspan=2)

# 参数说明:
{
	"colspan": 1,// 合并列数(固定为1)
	"col": 0, //起始第N列
	"rowspan":  3//合并行数
	"row": 0, //起始第N行
}


#输出对象数据:
{colspan=1, col=0, rowspan=3, row=0},
{colspan=1, col=0, rowspan=4, row=3},
{colspan=1, col=1, rowspan=2, row=0},
{colspan=1, col=1, rowspan=3, row=3}

核心代码逻辑:

import com.wiseco.model.mgt.server.web.vo.resp.ReportOutputDto;

import java.util.*;

public class MergeCells {
    
   /**
     * .
     * 构建返回对象
     *
     * @param varSummary    原始数据
     * @param targetColumns 动态指定需要合并的值
     * @return
     */
    public static List<Map<String, Integer>> buildMergeInfo(
            List<Map<String, Object>> varSummary,
            List<String> targetColumns) {

        List<Map<String, Integer>> result = new ArrayList<>();
        if (varSummary == null || varSummary.isEmpty() || targetColumns == null || targetColumns.isEmpty()) {
            return result;
        }

        int n = varSummary.size();
        int[] groupStarts = new int[targetColumns.size()];  // 每列当前分组的起始行索引
        String[] currentValues = new String[targetColumns.size()];  // 每列当前分组的值

        // 初始化第一行的值
        for (int col = 0; col < targetColumns.size(); col++) {
            currentValues[col] = getStringValue(varSummary.get(0), targetColumns.get(col));
        }

        // 遍历每一行(从第1行开始)
        for (int row = 1; row <= n; row++) {
            // 检查每列是否需要结束当前分组
            for (int col = 0; col < targetColumns.size(); col++) {
                String currentVal = (row < n) ?
                        getStringValue(varSummary.get(row), targetColumns.get(col)) :
                        null;

                // 如果列值变化或是最后一行
                boolean valueChanged = row < n && !Objects.equals(currentVal, currentValues[col]);
                if (valueChanged || row == n) {
                    int groupSize = row - groupStarts[col];
                    if (groupSize > 1) {
                        result.add(createCellInfo(groupStarts[col], col, groupSize, 1));
                    }

                    // 更新当前分组起始位置
                    groupStarts[col] = row;

                    // 重置当前值
                    if (row < n) {
                        currentValues[col] = currentVal;
                    }

                    // 当外层列值变化时,重置内层列的分组
                    for (int innerCol = col + 1; innerCol < targetColumns.size(); innerCol++) {
                        groupStarts[innerCol] = row;
                        if (row < n) {
                            currentValues[innerCol] = getStringValue(varSummary.get(row), targetColumns.get(innerCol));
                        }
                    }

                    // 跳出内层列循环,避免重复处理
                    break;
                }
            }
        }
        return result;
    }


	/**
     * .
     * 构建输出对象
     *
     * @param row     起始行
     * @param col     起始列
     * @param rowspan 合并行数
     * @param colspan 合并列数(固定值1)
     * @return
     */
    private static Map<String, Integer> createCellInfo(int row, int col, int rowspan, int colspan) {
        Map<String, Integer> cell = new HashMap<>();
        cell.put("row", row);
        cell.put("col", col);
        cell.put("rowspan", rowspan);
        cell.put("colspan", colspan);
        return cell;
    }


    private static String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return (value != null) ? value.toString() : null;
    }

}

测试案例:

public static void main(String[] args) {
        // 示例数据构造
        List<Map<String, Object>> varSummary = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("sub_product_name", "生活费-生意贷");
        map1.put("approval_result", "其它");
        map1.put("marital_state", 3);
        varSummary.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("sub_product_name", "生活费-生意贷");
        map2.put("approval_result", "其它");
        map2.put("marital_state", 3);
        varSummary.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("sub_product_name", "生活费-生意贷");
        map3.put("approval_result", "通过");
        map3.put("marital_state", 3);
        varSummary.add(map3);


        Map<String, Object> map4 = new HashMap<>();
        map4.put("sub_product_name", "生活费-联合贷");
        map4.put("approval_result", "通过");
        map4.put("marital_state", 3);
        varSummary.add(map4);

        Map<String, Object> map5 = new HashMap<>();
        map5.put("sub_product_name", "生活费-联合贷");
        map5.put("approval_result", "通过");
        map5.put("marital_state", 3);
        varSummary.add(map5);


        Map<String, Object> map6 = new HashMap<>();
        map6.put("sub_product_name", "生活费-联合贷");
        map6.put("approval_result", "通过");
        map6.put("marital_state", 3);
        varSummary.add(map6);

        Map<String, Object> map7 = new HashMap<>();
        map7.put("sub_product_name", "生活费-联合贷");
        map7.put("approval_result", "其他");
        map7.put("marital_state", 3);
        varSummary.add(map7);


        // 生成合并信息
        List<String> targetColumns = Arrays.asList("marital_state","sub_product_name");

        List<Map<String, Integer>> mergeInfo = buildMergeInfo(varSummary, targetColumns);
        System.out.println(varSummary);
        // 输出结果
        for (Map<String, Integer> cell : mergeInfo) {
            System.out.println(cell);
        }

        /* 原始数据格式
        [{sub_product_name=生活费-生意贷, approval_result=其它, marital_state=3},
        {sub_product_name=生活费-生意贷, approval_result=其它, marital_state=3},
        {sub_product_name=生活费-生意贷, approval_result=通过, marital_state=3},
        {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3},
        {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3},
        {sub_product_name=生活费-联合贷, approval_result=通过, marital_state=3},
        {sub_product_name=生活费-联合贷, approval_result=其他, marital_state=3}]
        
        构建输出对象
        {colspan=1, col=0, rowspan=3, row=0}
		{colspan=1, col=0, rowspan=4, row=3}
		{colspan=1, col=1, rowspan=2, row=0}
		{colspan=1, col=1, rowspan=3, row=3}*/
    }

总结

到此这篇关于Java根据分组key构建合并数据集的代码详解的文章就介绍到这了,更多相关Java根据key构建数据集内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot ResponseBody返回值处理的实现

    SpringBoot ResponseBody返回值处理的实现

    这篇文章主要介绍了SpringBoot ResponseBody返回值处理的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Spring Cloud 请求重试机制核心代码分析

    Spring Cloud 请求重试机制核心代码分析

    这篇文章主要介绍了Spring Cloud 请求重试机制核心代码分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 阿里面试Nacos配置中心交互模型是push还是pull原理解析

    阿里面试Nacos配置中心交互模型是push还是pull原理解析

    这篇文章主要为大家介绍了阿里面试Nacos配置中心交互模型是push还是pull原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 详解Java中的有参构造方法与无参构造方法

    详解Java中的有参构造方法与无参构造方法

    这篇文章主要详细介绍了Java中有参构造方法与无参构造方法,文中有详细的代码示例,让大家清晰明了的了解到有参构造方法与无参构造方法、以及应用,需要的朋友可以参考下
    2023-06-06
  • logback的AsyncAppender高效日志处理方式源码解析

    logback的AsyncAppender高效日志处理方式源码解析

    这篇文章主要为大家介绍了logback的AsyncAppender高效日志处理方式源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Java压缩文件操作详解

    Java压缩文件操作详解

    这篇文章主要为大家详细介绍了如何利用Java语言进行压缩文件操作,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-08-08
  • mybatis.type-aliases-package之巨坑的解决

    mybatis.type-aliases-package之巨坑的解决

    这篇文章主要介绍了mybatis.type-aliases-package之巨坑的解决,具有很好的参考价值,希望对大家有所帮助。
    2021-09-09
  • Java中try catch处理异常示例

    Java中try catch处理异常示例

    这篇文章主要给大家介绍了关于Java中try catch 的基本用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的(图文详解)

    Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的(图文详解)

    这篇文章主要介绍了Intellij IDEA根据maven依赖名查找它是哪个pom.xml引入的,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • SpringBoot 并发登录人数控制的实现方法

    SpringBoot 并发登录人数控制的实现方法

    这篇文章主要介绍了SpringBoot 并发登录人数控制的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05

最新评论