/** * * @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>>
时间: 2024-10-07 05:06:31