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构建数据集内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java后端限制频繁请求和重复提交的实现

    Java后端限制频繁请求和重复提交的实现

    很多用户会请求过于频繁或者是多次重复提交数据,本文主要介绍了Java后端限制频繁请求和重复提交的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Springboot Apollo配置yml的问题及解决方案

    Springboot Apollo配置yml的问题及解决方案

    这篇文章主要介绍了Springboot Apollo配置yml的问题及解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • Springboot整合Netty实现RPC服务器的示例代码

    Springboot整合Netty实现RPC服务器的示例代码

    这篇文章主要介绍了Springboot整合Netty实现RPC服务器的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java实现序列化与反序列化的简单示例

    Java实现序列化与反序列化的简单示例

    序列化与反序列化是指Java对象与字节序列的相互转换,一般在保存或传输字节序列的时候会用到,下面有两个Java实现序列化与反序列化的简单示例,不过还是先来看看序列和反序列化的具体概念:
    2016-05-05
  • 浅谈Java中的final关键字与C#中的const, readonly关键字

    浅谈Java中的final关键字与C#中的const, readonly关键字

    下面小编就为大家带来一篇浅谈Java中的final关键字与C#中的const, readonly关键字。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • 详解springboot中使用异步的常用两种方式及其比较

    详解springboot中使用异步的常用两种方式及其比较

    这篇文章主要介绍了详解springboot中使用异步的常用两种方式及其比较,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Java Stream流零基础教程

    Java Stream流零基础教程

    Java8的另一大亮点Stream,它与java.io包里的InputStream和OutputStream是完全不同的概念,下面这篇文章主要给大家介绍了关于Java8中Stream详细使用方法的相关资料,需要的朋友可以参考下
    2022-11-11
  • Java中随机函数变换的示例详解

    Java中随机函数变换的示例详解

    这篇文章主要为大家详细介绍了Java中随机函数的变换,文中的示例代码讲解详细,对我们学习Java有一定的帮助,感兴趣的可以了解一下
    2022-08-08
  • Java MySQL动态语句编写实现方式

    Java MySQL动态语句编写实现方式

    这篇文章主要介绍了Java MySQL动态语句编写实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2025-04-04
  • Spring的注解简单介绍

    Spring的注解简单介绍

    这篇文章主要介绍了Spring的注解简单介绍,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12

最新评论