多维数组集合遍历

组装数据:{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);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 504b 0304 1400 0008 0800 4498 963d a6fa9ff3 9f41 1e00 a88...
    BossOx阅读 11,385评论 0 0
  • 6270 a1da b5e7 b8aa 4d60 60c6 dea2 9fcc66b3 1acd 0596 77b...
    BossOx阅读 9,279评论 0 0
  • 504b 0304 1400 0008 0800 fa8c 963d 50740baa dffc 0e00 6cd...
    BossOx阅读 3,537评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,595评论 28 53
  • 人工智能是什么?什么是人工智能?人工智能是未来发展的必然趋势吗?以后人工智能技术真的能达到电影里机器人的智能水平吗...
    ZLLZ阅读 3,870评论 0 5