Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simply brute over all elements and skip duplicates (done by HashSet automatically). There are better ways to concat strings but for keeping it simple:</p> <pre><code>HashSet&lt;String&gt; result = new HashSet&lt;String&gt;(); for (String a : finalMap.get(0)) { for (String b : finalMap.get(1)) { for (String c : finalMap.get(2)) { for (String d : finalMap.get(3)) { result.add(a + b + c + d); } } } } System.out.println(result); </code></pre> <p>Output should be like<br> <code>[a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]</code> </p> <p>However if you do not know the total amount of lists yet or if they may vary you could use a recursive approach</p> <pre><code>void generate(HashSet&lt;String&gt; resultList, String resultString, int listNum, HashMap&lt;Integer, ArrayList&lt;String&gt;&gt; data) { if (listNum == 0) { // start: begin a new resultString for (int i = 0; i &lt; data.get(listNum).size(); i++) { generate(resultList, data.get(listNum).get(i), listNum + 1, data); } } else if (listNum == data.size() - 1) { // end: store completed resultStrings for (int i = 0; i &lt; data.get(listNum).size(); i++) { resultList.add(resultString + data.get(listNum).get(i)); } } else { // middlepart: append current string to given resultString for (int i = 0; i &lt; data.get(listNum).size(); i++) { generate(resultList, resultString + data.get(listNum).get(i), listNum + 1, data); } } } </code></pre> <p>called as follows:</p> <pre><code>HashSet&lt;String&gt; result = new HashSet&lt;String&gt;(); generate(result2, null, 0, finalMap); System.out.println(result); </code></pre> <p>Output should also be like<br> <code>[a3b2c1d1, a1b3c1d1, a1b1c1d1, a1b2c2d1, a4b1c2d1, a4b3c2d1, a1b2c1d1, a2b1c2d1, a2b2c2d1, a3b3c2d1, a1b1c2d1, a1b3c2d1, a2b3c2d1, a3b3c1d1, a2b2c1d1, a4b1c1d1, a4b3c1d1, a4b2c1d1, a3b1c2d1, a2b3c1d1, a3b1c1d1, a4b2c2d1, a2b1c1d1, a3b2c2d1]</code> </p>
    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. 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