I have 3 different sets of numbers, for example
a={1,3,4}
b={2,6}
c={0}
the size of sets can be variable, e.g. one set with 5 elements, another with 3, etc.
I have saved these values in a hashmap in java,
HashMap H=new HashMap<String,HashSet<Integer>>();
for this example, the keys of H are "a","b",and "c".
I want to make all possible combinations of the numbers in these three sets, that is:
1,2,0
1,6,0
3,2,0
3,6,0
4,2,0
4,6,0
But I have a problem. The number of sets can also be variable, i.e. my HashMap can have 3 to 10 keys. Do you have any idea how I should iterate over the hashmap, so that I can make all possible combinations?
ANSWER: I changed the HashMap to Vector, however, it also works with HashMap with little changes
call the function as follow:
Iterate_over_map(0,new Integer[h.size()],h,final_list);
Iterate_over_map(int count,Integer[] curr,Vector<HashSet<Integer>> h,Vector<Integer[]> final_list)
{
if(count>=h.size())
{
final_list.addElement(curr.clone());
return;
}
int next_count=count+1;
for (Integer element:h.elementAt(count))
{
curr[count]=element;
Iterate_over_map(next_count,curr,h,final_list);
}
}
OLD solution
for(int x:H.get("a"))
{
v[0]=x;//node1 (an ortholog of node1)
for(int y:H.get("b"))
{
v[1]=y;//node2 (an ortholog of node2)
for(int z:H.get("c"))
{
v[2]=z;//node3 (an ortholog of node3)
}
}
}
thanks a lot.