Я считаю, что решить вопрос
public class Game {
public static void main(String... args) {
List<Competitor> listCompetitors = new ArrayList<>();
listCompetitors.add(new Competitor(1, "Cat 00", 93));
listCompetitors.add(new Competitor(1, "Cat 10", 11));
listCompetitors.add(new Competitor(1, "Cat 23", 20));
listCompetitors.add(new Competitor(2, "Dog 61", 54));
listCompetitors.add(new Competitor(2, "Dog 18", 40));
listCompetitors.add(new Competitor(2, "Dog 45", 71));
listCompetitors.add(new Competitor(2, "Dog 30", 68));
listCompetitors.add(new Competitor(3, "Pig 90", 90));
listCompetitors.add(new Competitor(3, "Pig 78", 32));
listCompetitors.add(new Competitor(4, "Cow 99", 90));
List<Integer> typeList = new ArrayList<>(listCompetitors.stream()
.map(Competitor::getType)
.collect(Collectors.toSet()));
int sizeLimit = typeList.size();
List<List<Competitor>> listOfListCompetitor = new ArrayList<>();
getListCompetitorCombination(new ArrayList<>(), listCompetitors, sizeLimit, listOfListCompetitor);
listOfListCompetitor.stream().forEach(listCompetitor -> {
System.out.println("");
listCompetitor.stream().sorted(Comparator.comparing(Competitor::getType)).forEach(System.out::print);
});
System.out.println();
}
public static List<Competitor> getListCompetitorCombination(List<Competitor> processed, List<Competitor> sublistCompetitor, int sizeLimit, List<List<Competitor>> outCompetitor) {
List<Competitor> listCompetitorCombination = new ArrayList<>();
/*
List<Integer> typeList = new ArrayList<>(sublistCompetitor.stream()
.map(Competitor::getType)
.collect(Collectors.toSet()));
*/
int type = sublistCompetitor.get(0).getType();//typeList.stream().findFirst().orElse(0);
List<Competitor> listCompetitorsIncludeType = sublistCompetitor
.stream()
.filter(competitor -> competitor.getType() == type)
.collect(Collectors.toList());
List<Competitor> listCompetitorsExcludeType = sublistCompetitor
.stream()
.filter(competitor -> competitor.getType() != type)
.collect(Collectors.toList());
listCompetitorsIncludeType.stream().forEach(
competitor
-> {
List<Competitor> newProcessed = new ArrayList<>(processed);
newProcessed.add(0, competitor);
if (sizeLimit == newProcessed.size()) {
outCompetitor.add(newProcessed);
} else {
getListCompetitorCombination(newProcessed, listCompetitorsExcludeType, sizeLimit, outCompetitor);
}
});
return listCompetitorCombination;
}
}
Мой вывод
debug:
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 00, power=93} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 10, power=11} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 61, power=54} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 18, power=40} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 45, power=71} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 90, power=90} Competitor{type=4, name=Cow 99, power=90}
Competitor{type=1, name=Cat 23, power=20} Competitor{type=2, name=Dog 30, power=68} Competitor{type=3, name=Pig 78, power=32} Competitor{type=4, name=Cow 99, power=90}
BUILD SUCCESSFUL (total time: 1 second)
Теперь, Может кто-нибудь предложить другое оптимизированное решение?