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

报表需求改动需要对接口返回结果List<Map<String, String>>进行行转列,以下为测试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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值