List<Map<String, String>>数据行转列

报表需求改动需要对接口返回结果List>进行行转列,以下为测试demo:

package com.gykjit.spd.system.service.impl;


import java.util.*;

public class RowToColumn {

    public static void main(String[] args) {
        List<Map<String, String>> rows = new ArrayList<>();
        Map<String, String> row1 = new HashMap<>();
        row1.put("Name", "Alice");
        row1.put("Age", "30");
        row1.put("sex", "1");

        Map<String, String> row2 = new HashMap<>();
        row2.put("Name", "Bob");
        row2.put("Age", "25");
        row2.put("sex", "0");
        rows.add(row1);
        rows.add(row2);

        List<Map<String, String>> columns = new ArrayList<>();

        Set<String> keySet = row1.keySet();
        for (String oldKey : keySet) {
            Map<String, String> columnMap = new HashMap<>();
            for (int i = 0; i < rows.size(); i++) {
                final int index = i;
                String columnName = "row_" + index;
                String columnValue = rows.get(index).get(oldKey);
                columnMap.put(columnName, columnValue);
            }
            columns.add(columnMap);
        }
        System.out.println(rows);
        System.out.println(columns);
    }
}


你可能感兴趣的:(工作工具,list,java,windows)