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}