文章目錄
- 前言
- 背景
- 總結
前言
請各大網友尊重本人原創知識分享,謹記本人博客:南國以南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記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文鏈接!!!