文章目录
- 前言
- 背景
- 总结
前言
请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
提示:以下是本篇文章正文内容,下面案例可供参考
背景
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中的数据,需要构建合并对象属性
核心逻辑说明:
- 双层分组处理:
外层分组:按 sub_product_name 分组(示例中3条数据均相同)
内层分组:在外层分组内按 approval_result 分组(示例中前两条"其它"相同,第三条"通过"不同)
- 合并规则:
第一列(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}*/}
总结
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!