首页 > 代码库 > Map<String,List<String>>转为List<Map<String,String>>
Map<String,List<String>>转为List<Map<String,String>>
/** * * @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);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。