组装数据:{0,【000,001,002,003】}, {1,【110,111,112,113】}, {2,【220,221,222,223】}
Map<Integer, HashSet<String>> hashMapMap = null;
int count = 0;
public void testMain() {
hashMapMap = new HashMap<>();
count = 2;
for (int i = 0; i < count; i++) {
HashSet<String> demoSet = new HashSet<>();
String res = "";
switch (i) {
case 0:
res = "00";
break;
case 1:
res = "11";
break;
case 2:
res = "22";
break;
case 3:
res = "33";
break;
}
for (int j = 0; j < 2; j++) {
String value = res + j;
demoSet.add(value);
}
hashMapMap.put(i, demoSet);
}
test2(0, new LinkedList<String>());
}
private void test2(int index, LinkedList<String> centerSet) {
if (centerSet == null) {
centerSet = new LinkedList<>();
}
HashSet<String> hashSet = hashMapMap.get(index);
for (String next : hashSet) {
// 遍历循环加入缓存集合直到最后一个集合
if (index < count - 1) {
centerSet.add(next);
}
// 如果最后一个集合,打印缓存集合中的数据, 和当前最后一个集合中的数据
if (index == count - 1) {
for (String centerNext : centerSet) {
System.out.print(centerNext + ",");
}
System.out.print(next + ",");
}
if (index + 1 < count) {
test2(index + 1, centerSet);
}
// 最后一个数据换行
if (index == count - 1) {
System.out.println("");
}
}
//当前最后一个集合遍历完后,移除前一个缓存数据, 上一级循环第一个数据遍历完成,移除上一级缓存数据,重新加入第二个数据,继续遍历最后一个循环
if (index - 1 >= 0) {
centerSet.remove(index - 1);
}
}