将Map<String,List<String>>转换为List<Map<String, String>>

 public static  List<Map<String, String>> getListMap(Map<String, List<String>> map, Map<String,String> mapone, int offset,
                                                        List<Map<String,String>> paramListMap) {
        String keys[] = map.keySet().toArray(new String[]{});
        if (offset<keys.length-1){
            List<String> vl1 = map.get(keys[offset]);
            for (String v:vl1) {
                mapone.put(keys[offset],v);
                getListMap(map,mapone,offset+1,paramListMap);
            }

        }else if (offset==keys.length-1){
            List<String> vl1 = map.get(keys[offset]);
            for (String v:vl1) {
                Map<String,String> map1 = new HashMap<>();
                map1.put(keys[offset],v);
                map1.putAll(mapone);
                paramListMap.add(map1);
            }
        }
        return paramListMap;
    }

测试:

public static void main(String[] args) {
        Map<String,List<String>> test = new HashMap<>();
        List<String> list1 = new ArrayList<>();
        list1.addAll(Arrays.asList(new String[] {"1","2","3","4"}));
        test.put("KEY1",list1);
        list1 = new ArrayList<>();
        list1.addAll(Arrays.asList(new String[] {"a","b","c"}));
        test.put("KEY2",list1);
        list1 = new ArrayList<>();
        list1.addAll(Arrays.asList(new String[] {"A","B"}));
        test.put("KEY3",list1);

        List<Map<String, String>> list = getListMap(test,new HashMap<>(),0,new ArrayList<>());
        for (Map<String, String> param:list) {
            System.out.print("{"+sortParameters(param)+"}\n");
        }

    }
    public static String sortParameters(Map<String, String> map) {
        Set<String> keys = map.keySet();
        List<String> paramsBuf = new ArrayList<String>();
        for (String k : keys) {
            paramsBuf.add(k + "=" + map.get(k));
        }
        // 对参数排序
        Collections.sort(paramsBuf);
        String result="";
        int count=paramsBuf.size();
        for(int i=0;i<count;i++){
            if(i<(count-1)){
                result+=paramsBuf.get(i)+"  ";
            }else {
                result+=paramsBuf.get(i);
            }
        }
        return result;
    }

运行结果:

{KEY1=1  KEY2=a  KEY3=A}
{KEY1=1  KEY2=a  KEY3=B}
{KEY1=2  KEY2=a  KEY3=A}
{KEY1=2  KEY2=a  KEY3=B}
{KEY1=3  KEY2=a  KEY3=A}
{KEY1=3  KEY2=a  KEY3=B}
{KEY1=4  KEY2=a  KEY3=A}
{KEY1=4  KEY2=a  KEY3=B}
{KEY1=1  KEY2=b  KEY3=A}
{KEY1=1  KEY2=b  KEY3=B}
{KEY1=2  KEY2=b  KEY3=A}
{KEY1=2  KEY2=b  KEY3=B}
{KEY1=3  KEY2=b  KEY3=A}
{KEY1=3  KEY2=b  KEY3=B}
{KEY1=4  KEY2=b  KEY3=A}
{KEY1=4  KEY2=b  KEY3=B}
{KEY1=1  KEY2=c  KEY3=A}
{KEY1=1  KEY2=c  KEY3=B}
{KEY1=2  KEY2=c  KEY3=A}
{KEY1=2  KEY2=c  KEY3=B}
{KEY1=3  KEY2=c  KEY3=A}
{KEY1=3  KEY2=c  KEY3=B}
{KEY1=4  KEY2=c  KEY3=A}
{KEY1=4  KEY2=c  KEY3=B}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值