I have a java Hash, the structure is like this:
HashMap<Integer, ArrayList<String>> finalMap = new HashMap<Integer, ArrayList<String>>();
The finalMap.toString() is some thing like this
{0=[a1, a2, a3, a4], 1=[b1, b2, b3], 2=[c1, c2], 3=[d1]}
I need to generate all combination like this:
1. a1 b1 c1 d1
2. a1 b1 c2 d1
3. a1 b2 c1 d1
4. a1 b2 c2 d1
5. a1 b3 c1 d1
6. a1 b3 c2 d1
...
...
...
...
...
Thanks in advance.
Simply brute over all elements and skip duplicates (done by HashSet automatically). There are better ways to concat strings but for keeping it simple:
Output should be like
[a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]However if you do not know the total amount of lists yet or if they may vary you could use a recursive approach
called as follows:
Output should also be like
[a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]