title: All possible combinations of N elements from one list, with M elements from another list
body: <p>I want to make a method in java that receives two String Lists: <code>list1</code>, <code>list2</code>, and two integers: <code>i1</code> and <code>i2</code>. The method has to return a list of lists (<code>List&lt;List&lt;String&gt;&gt;</code>) with all possible distinct list combinations of (<code>i1</code>+<code>i2</code>) elements, so that these combinations has <code>i1</code> elements from <code>list1</code>, and <code>i2</code> elements from <code>list2</code>.</p>
  
  <p>Examples:</p>
  
  <pre><code>list1 = {A,B,C};  list2 = {1,2,3,4};
  
  example1:
  i1 = 1;           i2 = 3;
  method(lis1,i1,list2,i2) results:
  {{A,1,2,3};{A,2,3,4};{A,1,3,4};{A,1,2,3}
  {B,1,2,3};{B,2,3,4};{B,1,3,4};{B,1,2,3}
  {C,1,2,3};CA,2,3,4};{C,1,3,4};{C,1,2,3}}
  ////////////////////////////////////////////////////////////////////
  
  example2:
  i1 = 2;           i2 = 2;
  
  method(lis1,i1,list2,i2) results:
  {{A,B,1,2};{A,B,1,3};{A,B,1,4};{A,B,2,3};{A,B,2,4};{A,B,3,4};
  {A,C,1,2};{A,C,1,3};{A,C,1,4};{A,C,2,3};{A,C,2,4};{A,C,3,4};
  {B,C,1,2};{B,C,1,3};{B,C,1,4};{B,C,2,3};{B,C,2,4};{B,C,3,4}}
  ///////////////////////////////////////////////////////////////
  
  example3:
  i1 = 2;           i2 = 1;
  
  method(lis1,i1,list2,i2) results:
  {{A,B,1};{A,B,2};{A,B,3};{A,B,4}
  {A,C,1};{A,C,2};{A,C,3};{A,C,4};
  {B,C,1};{B,C,2};{B,C,3};{B,C,4};}
  ///////////////////////////////////////////////////////////////
  
  
  example4:
  i1 = 0;           i2 = 1;
  
  method(lis1,i1,list2,i2) results:
  {{1};{2};{3};{4}}
  ///////////////////////////////////////////////////////////////
  </code></pre>
  
  <p>-Lists has not duplicate elements.</p>
  
  <p>-Elements in one list do not appear in the other one.</p>
  
  <p>-I don't need two different lists with same elements:(if i have {A,1} i don't need {1,A}).</p>
  
  <hr>
  
  <p>My current solution only works with fixed size of <code>i1</code> and <code>i2</code>, and i adapt it from this question: ( <a href="https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n">Algorithm to return all combinations of k elements from n</a> )</p>
  
  <p>Can anybody please tell me any algorithm or structure for this problem?.</p>
  
  <p>thanks!</p>
  
  <hr>
  
  <p>EDIT: (added some of my code)</p>
  
  <pre><code>//This method returns a list containing, all possible distinct combinations
  //of 3 items from the elemnts of "someList".
  private List&lt;List&lt;String&gt;&gt; combinationOfThree(List&lt;String&gt; someList){
       List&lt;List&lt;String&gt;&gt; toReturn = new ArrayList&lt;&gt;();
       List&lt;String&gt; oneCombination = new ArrayList&lt;&gt;();
       int i, j, k;
       int len = someList.size();
  
       if (len&lt;=3){
           for(String s :someList){
               oneCombination.add(s);
           }
           toReturn.add(oneCombination);
       }
       else{
           for (i = 0; i &lt; len - 2; i++){
               for (j = i + 1; j &lt; len - 1; j++){
                   for (k = j + 1; k &lt; len; k++){
                       oneCombination = new ArrayList&lt;&gt;();
                       oneCombination.add(someList.get(i));
                       oneCombination.add(someList.get(j));
                       oneCombination.add(someList.get(k));
                       toReturn.add(oneCombination);
                   }
               }
           }
       }
   return toReturn;
  }
  
  private List&lt;List&lt;String&gt;&gt; allPosibleCombinations(List&lt;String&gt; list1, int list1_Amount, List&lt;String&gt; list2, int list2_Amount){
      List&lt;List&lt;String&gt;&gt; toReturn = new ArrayList&lt;&gt;();
      //currently i can only make combinations of 3 items. 
      //I can implement "combinationOfTwo" , "combinationOfFour" and so on, but it is nasty as hell.
      if (list1_Amount == list2_Amount == 3){
          List&lt;List&lt;String&gt;&gt; combinationOfThreeFromList1 = combinationOfThree(list1);
          List&lt;List&lt;String&gt;&gt; combinationOfThreeFromList2 = combinationOfThree(list2);
  
      for (List&lt;String&gt; a_l1_comb : combinationOfThreeFromList_1){
          for (List&lt;String&gt; a_l2_comb : combinationOfThreeFromList_2){
              toReturn.add(appendLists(a_l1_comb , a_l2_comb);
          }
      }
  }
  return toReturn;
  }
  </code></pre>