Suppose I have 3 lists: ['q','w'], ['a','s'], ['z','x']. How to get a list of possible combinations out of these lists? So I get a list [['q','a','z'],['q','s','z']] and such. I made a method for two, but can't figure one for N lists:
static <E> ArrayList combine(ArrayList<E> one,ArrayList<E> two)
{
ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>();
for(E e:one)
{
for(E e2:two)
{
ArrayList ps=new ArrayList();
ps.add(e);
ps.add(e2);
combs.add(ps);
}
}
return combs;
}
I found out that this is done by Guava's Sets.cartesianProduct.
See Question&Answers more detail:os