Java 在数集合上迭代

Java 在数集合上迭代,java,hashmap,Java,Hashmap,我有3组不同的数字,例如 a={1,3,4} b={2,6} c={0} 集合的大小可以是可变的,例如一个集合包含5个元素,另一个集合包含3个元素,等等 我已经在java的hashmap中保存了这些值 HashMap H=new HashMap<String,HashSet<Integer>>(); 非常感谢。您应该使用递归函数。我举个例子 public static void main(String[] args) { String[] keys = map.

我有3组不同的数字,例如
a={1,3,4}
b={2,6}
c={0}

集合的大小可以是可变的,例如一个集合包含5个元素,另一个集合包含3个元素,等等

我已经在java的hashmap中保存了这些值

HashMap H=new HashMap<String,HashSet<Integer>>();

非常感谢。

您应该使用递归函数。我举个例子

public static void main(String[] args) {
    String[] keys = map.keySet().toArray(new String[0]);
    loopMap(map, keys, 0, "");
}

public static void loopMap(Map<String, Set<Integer>> map, String[] keys, int index, String res) {
    if (index == keys.length) {
        System.out.println(res);
        return;
    }
    Set<Integer> set = map.get(keys[index]);

    for(Integer ele : set) {
        loopMap(map, keys, index + 1, res + ele);
    }
}
publicstaticvoidmain(字符串[]args){
String[]keys=map.keySet().toArray(新字符串[0]);
loopMap(映射,键,0,“”);
}
公共静态void loopMap(映射映射,字符串[]键,int索引,字符串res){
if(index==keys.length){
系统输出打印项次(res);
回来
}
Set=map.get(键[索引]);
for(整数元素:集合){
loopMap(映射、键、索引+1、res+ele);
}
}

使用:map use LinkedHashMap,set为LinkedHashSet并选中null^^

您需要使用递归而不是嵌套循环,这将实现您想要的功能:

public static void main(String[] args) 
{
  List<List<Integer>> integers = new ArrayList<List<Integer>>();
  integers.add(Arrays.asList(1, 3, 4));
  integers.add(Arrays.asList(2, 6));
  integers.add(Arrays.asList(0));

  List<List<Integer>> combinations = combine(integers);

  System.out.println(combinations);
}

private static List<List<Integer>> combine(List<List<Integer>> allIntegers) 
{
  List<Integer> integers = allIntegers.remove(0);
  List<List<Integer>> allCombinations = new ArrayList<List<Integer>>();

  for (Integer i : integers) 
  {
    if (allIntegers.isEmpty()) 
    {
      allCombinations.add(new ArrayList<Integer>(Arrays.asList(i)));
    }
    else 
    {
      for (List<Integer> combinations : combine(new ArrayList<List<Integer>>(allIntegers))) 
      {
        combinations.add(0, i);
        allCombinations.add(combinations);
      }
    }
  }

  return allCombinations;
}

不,这不是家庭作业。这是我项目的一小部分;谢谢你的回答。我知道如何迭代hashmap。我不知道如何实现我提到的输出。首先,我为循环编写了3个,每个循环迭代一个键。但是当节点数超过3时,我应该始终增加s的数量!嘿谢谢。我没有想到递归。再次感谢,谢谢你抽出时间。这个解决方案有效。
public static void main(String[] args) {
    String[] keys = map.keySet().toArray(new String[0]);
    loopMap(map, keys, 0, "");
}

public static void loopMap(Map<String, Set<Integer>> map, String[] keys, int index, String res) {
    if (index == keys.length) {
        System.out.println(res);
        return;
    }
    Set<Integer> set = map.get(keys[index]);

    for(Integer ele : set) {
        loopMap(map, keys, index + 1, res + ele);
    }
}
public static void main(String[] args) 
{
  List<List<Integer>> integers = new ArrayList<List<Integer>>();
  integers.add(Arrays.asList(1, 3, 4));
  integers.add(Arrays.asList(2, 6));
  integers.add(Arrays.asList(0));

  List<List<Integer>> combinations = combine(integers);

  System.out.println(combinations);
}

private static List<List<Integer>> combine(List<List<Integer>> allIntegers) 
{
  List<Integer> integers = allIntegers.remove(0);
  List<List<Integer>> allCombinations = new ArrayList<List<Integer>>();

  for (Integer i : integers) 
  {
    if (allIntegers.isEmpty()) 
    {
      allCombinations.add(new ArrayList<Integer>(Arrays.asList(i)));
    }
    else 
    {
      for (List<Integer> combinations : combine(new ArrayList<List<Integer>>(allIntegers))) 
      {
        combinations.add(0, i);
        allCombinations.add(combinations);
      }
    }
  }

  return allCombinations;
}
[[1, 2, 0], [1, 6, 0], [3, 2, 0], [3, 6, 0], [4, 2, 0], [4, 6, 0]]