Java根据分组key构建合并数据集的代码详解
目录
- 背景
- 总结
背景android
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[androidtargetColumns.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", phpcol); 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 php= 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_resulwww.devze.comt=其他, 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构建数据集内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论