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

本文介绍了如何在Java中处理报表需求,通过RowToColumn类的示例展示了如何将接口返回的结果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);
    }
}


将 `List<Map<String, String>>` 转换为 `Map` 可以根据不同的需求采用不同的转换逻辑,以下是几种常见的转换方式: ### 方式一:将 `List<Map<String, String>>` 转换为 `Map<String, List<String>>`,按不同的 key 进行分组,将相同 key 的 value 存储到 list 中 ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ListMapToMapExample { public static void main(String[] args) { HashMap<String, String> map1 = new HashMap<>(); HashMap<String, String> map2 = new HashMap<>(); HashMap<String, String> map3 = new HashMap<>(); map1.put("productid", "123"); map1.put("id", "111"); map2.put("productid", "123"); map2.put("id", "1234"); map3.put("productid", "222"); map3.put("id", "333"); List<Map<String, String>> list = new ArrayList<>(); list.add(map1); list.add(map2); list.add(map3); Map<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(map -> map.get("productid").toString(), Collectors.mapping(map -> map.get("id").toString(), Collectors.toList()))); System.out.println(collect); } } ``` 这种方式使用了 Java 8 的 Stream API 中的 `Collectors.groupingBy` 和 `Collectors.mapping` 方法,将 `List<Map<String, String>>` 按 `productid` 进行分组,相同 `productid` 的 `id` 值存储在一个 `List` 中,最终存储在 `Map` 里,与引用[2]的思路一致。 ### 方式二:将 `List<Map<String, String>>` 转换为 `Map<String, Map<String, String>>` ```java import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ListMapToMapExample2 { public static void main(String[] args) { // 假设这里有一个 query 的 List<Map<String, String>> List<Map<String, String>> query = new java.util.ArrayList<>(); LinkedHashMap<String, LinkedHashMap<String, String>> testItem = query.stream().collect(Collectors.groupingBy(p -> p.get("PROJECT_TYPE"))) .entrySet().stream().collect( Collectors.toMap((p) -> p.getKey().equals("1")?"结果1":"结果2", (p1) -> p1.getValue().stream(). sorted(java.util.Comparator.comparing(s -> Integer.parseInt(org.apache.commons.lang3.StringUtils.ifNull(s.get("ORDER_NUMBER"),"999").toString()))). collect(Collectors.toList()).stream().collect(Collectors.toMap((p) -> p.get("PROJECT_CODE"), (p2) -> p2.get("PROJECT_NAME"), (k, k1) -> k, LinkedHashMap::new)), (k, k1) -> k, LinkedHashMap::new)); } } ``` 此方式使用 Stream 流将 `List<Map<String, String>>` 转换为 `Map<String, Map<String, String>>`,具体的分组和映射规则可以根据 `PROJECT_TYPE`、`PROJECT_CODE`、`PROJECT_NAME` 等键进行操作,参考了引用[4]的实现思路。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值