码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-25 11:42:21      阅读:524      评论:0      收藏:0      [点我收藏+]

标签:java   for   re   c   代码   ar   new   size   

/**
 * 
 * @param map  输入
 * @param list 输出
 * @param idx     次序
 * @param pathMap 已选
 */
void map2List(Map<String, List<String>> map, List<Map<String, String>> list,
                                    int idx, HashMap<String,String> pathMap){
    if(null == pathMap){
        pathMap = new HashMap<String,String>();
    }
    
    String[] products = map.keySet().toArray(new String[] {});
    Arrays.sort(products);
    
    if (idx + 1 < products.length) {
        List<String> eleList = map.get(products[idx]);
        for (int i = 0; i < eleList.size(); i++) {
            pathMap.put(products[idx], eleList.get(i));
            
            map2List(map, list, idx + 1, pathMap);
        }
    } else if (idx + 1 == products.length) {
        List<String> eleList = map.get(products[idx]);
        for (int i = 0; i < eleList.size(); i++) {
            pathMap.put(products[idx], eleList.get(i));
            
            list.add((Map<String,String>)pathMap.clone());
        }
    }
}

测试代码:

HashMap<String, List<String>> map = new HashMap<String, List<String>>();
List<String> a = new ArrayList<String>();
a.add("a1");
a.add("a2");
a.add("a3");
map.put("a", a);
List<String> b = new ArrayList<String>();
b.add("b1");
b.add("b2");
map.put("b", b);
List<String> c = new ArrayList<String>();
c.add("c1");
map.put("c", c);
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
map2List(map, list, 0, null);
System.out.println(list);


Map<String,List<String>>转为List<Map<String,String>>,布布扣,bubuko.com

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

标签:java   for   re   c   代码   ar   new   size   

原文地址:http://my.oschina.net/h2do/blog/294727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!