I have created a function in Java to find all possible combination with lists. I created this code only for 2 skills, but since the number of skills may change dynamically, I need to change my code to support a dynamic number of nested loops to find the combination of experts for skills.
public List<ProjectAndTeam> teamCombinations(List<ProjectAndTeam> projectAndTeams) {
List<ProjectAndTeam> allTeamCombinations = new ArrayList<>();
for (ProjectAndTeam currentProjectTeam : projectAndTeams) {
ProjectAndTeam projectAndTeam = new ProjectAndTeam();
projectAndTeam.project = currentProjectTeam.project;
for (int i = 0; i < currentProjectTeam.expertForSkill.get(0).expertList.size(); i++) {
for (int j = 0; j < currentProjectTeam.expertForSkill.get(1).expertList.size(); j++) {
ExpertForSkill expertForSkill = new ExpertForSkill();
expertForSkill.skill = currentProjectTeam.expertForSkill.get(0).skill;
expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(0).expertList.get(i));
ExpertForSkill expertForSkillSecond = new ExpertForSkill();
expertForSkillSecond.skill = currentProjectTeam.expertForSkill.get(1).skill;
expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(1).expertList.get(j));
projectAndTeam.expertForSkill.add(expertForSkill);
projectAndTeam.expertForSkill.add(expertForSkillSecond);
}
}
allTeamCombinations.add(projectAndTeam);
}
return allTeamCombinations;
}
Here are my ProjectAndTeam, ExprtForSkill and Expert classes
public class ProjectAndTeam {
int id;
Project project;
List<ExpertForSkill> expertForSkill = new ArrayList<>();
double totalSalary;
double totalProductivity;
}
public class ExpertForSkill {
String skill;
List<Expert> expertList = new ArrayList<>();
}
public class Expert {
int id;
List<String> skills = new ArrayList<>();
int capacity;
double productivity;
double salary;
}
How can I get all combinations with a varying number of nested loops?
I believe that I have to write a recursion function to handle it but I'm confused.