I want to make a method in java that receives two String Lists: list1
, list2
, and two integers: i1
and i2
. The method has to return a list of lists (List<List<String>>
) with all possible distinct list combinations of (i1
+i2
) elements, so that these combinations has i1
elements from list1
, and i2
elements from list2
.
Examples:
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}}
///////////////////////////////////////////////////////////////
-Lists has not duplicate elements.
-Elements in one list do not appear in the other one.
-I don't need two different lists with same elements:(if i have {A,1} i don't need {1,A}).
My current solution only works with fixed size of i1
and i2
, and i adapt it from this question: ( Algorithm to return all combinations of k elements from n )
Can anybody please tell me any algorithm or structure for this problem?.
thanks!
EDIT: (added some of my code)
//This method returns a list containing, all possible distinct combinations
//of 3 items from the elemnts of "someList".
private List<List<String>> combinationOfThree(List<String> someList){
List<List<String>> toReturn = new ArrayList<>();
List<String> oneCombination = new ArrayList<>();
int i, j, k;
int len = someList.size();
if (len<=3){
for(String s :someList){
oneCombination.add(s);
}
toReturn.add(oneCombination);
}
else{
for (i = 0; i < len - 2; i++){
for (j = i + 1; j < len - 1; j++){
for (k = j + 1; k < len; k++){
oneCombination = new ArrayList<>();
oneCombination.add(someList.get(i));
oneCombination.add(someList.get(j));
oneCombination.add(someList.get(k));
toReturn.add(oneCombination);
}
}
}
}
return toReturn;
}
private List<List<String>> allPosibleCombinations(List<String> list1, int list1_Amount, List<String> list2, int list2_Amount){
List<List<String>> toReturn = new ArrayList<>();
//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<List<String>> combinationOfThreeFromList1 = combinationOfThree(list1);
List<List<String>> combinationOfThreeFromList2 = combinationOfThree(list2);
for (List<String> a_l1_comb : combinationOfThreeFromList_1){
for (List<String> a_l2_comb : combinationOfThreeFromList_2){
toReturn.add(appendLists(a_l1_comb , a_l2_comb);
}
}
}
return toReturn;
}
>`. You call it twice, once for each list. You then create a method to generate all combinations of two `List
>`, where result is a list of values, each list-value being a concatenation of a list-value from list 1 with a list-value from list 2.