Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at the following two methods, they do exactly what you asked for. I wrote them to be generic, it doesn't matter how long your lists are or how many keys exist in the map, the combinations generated are correct.</p> <p>The code below is <em>iterative</em>, based on the algorithm of Python's <a href="http://docs.python.org/library/itertools.html#itertools.product" rel="nofollow"><code>itertools.product()</code></a> function for calculating the Cartesian product of a list of lists.</p> <pre><code>public String[][] allUniqueCombinations() { List&lt;String&gt; labels = new ArrayList&lt;String&gt;(); List&lt;List&lt;String&gt;&gt; lists = new ArrayList&lt;List&lt;String&gt;&gt;(); for (Map.Entry&lt;String, Vector&lt;String&gt;&gt; entry : dataStructure.entrySet()) { labels.add(entry.getKey()); lists.add(entry.getValue()); } List&lt;List&lt;String&gt;&gt; combinations = product(lists); int m = combinations.size() + 1; int n = labels.size(); String[][] answer = new String[m][n]; for (int i = 0; i &lt; n; i++) answer[0][i] = labels.get(i); for (int i = 1; i &lt; m; i++) for (int j = 0; j &lt; n; j++) answer[i][j] = combinations.get(i-1).get(j); return answer; } private List&lt;List&lt;String&gt;&gt; product(List&lt;List&lt;String&gt;&gt; lists) { List&lt;List&lt;String&gt;&gt; result = new ArrayList&lt;List&lt;String&gt;&gt;(); result.add(new ArrayList&lt;String&gt;()); for (List&lt;String&gt; e : lists) { List&lt;List&lt;String&gt;&gt; tmp1 = new ArrayList&lt;List&lt;String&gt;&gt;(); for (List&lt;String&gt; x : result) { for (String y : e) { List&lt;String&gt; tmp2 = new ArrayList&lt;String&gt;(x); tmp2.add(y); tmp1.add(tmp2); } } result = tmp1; } return result; } </code></pre> <p>I tested them with the example in the question:</p> <pre><code>LinkedHashMap&lt;String, Vector&lt;String&gt;&gt; sample = new LinkedHashMap&lt;String, Vector&lt;String&gt;&gt;(); Vector&lt;String&gt; v1 = new Vector&lt;String&gt;(); v1.add("1"); v1.add("2"); v1.add("3"); Vector&lt;String&gt; v2 = new Vector&lt;String&gt;(); v2.add("3"); v2.add("2"); Vector&lt;String&gt; v3 = new Vector&lt;String&gt;(); v3.add("5"); v3.add("6"); v3.add("7"); sample.put("foo", v1); sample.put("bar", v2); sample.put("baz", v3); Foo foo = new Foo(sample); String[][] ans = foo.allUniqueCombinations(); for (String[] row : ans) System.out.println(Arrays.toString(row)); </code></pre> <p>The answer that gets printed is the expected (although the combinations appear in a different order):</p> <pre><code>[foo, bar, baz] [1, 3, 5] [1, 3, 6] [1, 3, 7] [1, 2, 5] [1, 2, 6] [1, 2, 7] [2, 3, 5] [2, 3, 6] [2, 3, 7] [2, 2, 5] [2, 2, 6] [2, 2, 7] [3, 3, 5] [3, 3, 6] [3, 3, 7] [3, 2, 5] [3, 2, 6] [3, 2, 7] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload