Если кто-то ищет более гибкое решение, я создаю его с использованием потока и уменьшаю. Вы можете видеть, что он работает здесь https://ideone.com/sSRrY3.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Combination {
private List<List<List<String>>> pairs = new ArrayList<>();
public Combination(List<String> elements) {
this.pairs = createPairsFromElements(elements);
}
private Combination() {
}
public static Combination createFromPairs(String[][] arrPairs) {
Combination combination = new Combination();
combination.pairs = createPairsFromArrayPairs(arrPairs);
return combination;
}
/**
* Create the pairs based on the String[][]
*
* Add the empty option at the beginner of each group
* Create the Lists and group them again
*
* @param arrPairs
* @return pairs
*/
private static List<List<List<String>>> createPairsFromArrayPairs(String[][] arrPairs) {
List<List<List<String>>> pairs = new ArrayList<>();
for (String[] groupValue: arrPairs) {
List<List<String>> listGroupValue = new ArrayList<>();
/* add the empty value to the pairs */
listGroupValue.add(Arrays.asList());
for (String value: groupValue) {
List<String> listValue = Arrays.asList(value);
listGroupValue.add(listValue);
}
pairs.add(listGroupValue);
}
return pairs;
}
/**
* Convert the string list "a", "b", "c" to
* this:
* [
* [ [], [a] ], [ [], [b] ], [ [], [c] ]
* ]
*
* @param elements List of values to the collection
* @return pairs
*/
private static List<List<List<String>>> createPairsFromElements(List<String> elements) {
return elements.stream().map(
(String value) -> {
List<List<String>> values = Arrays.asList(
Arrays.asList(), // immutable empty list
Arrays.asList(value) // immutable atomic list
);
return values;
}
).collect(Collectors.toList());
}
/**
* Get the combination without restriction
*
* @param size
* @return
*/
public List<List<String>> getCombination(int size) {
return this.getCombination(size, false);
}
/**
/*
* Combine every pair of elements creating a big list
* [ [], [b] ] x [ [], [a] ] = [ ([] + []), ([] + [a]), ([b] + []), ([b] + [a])) ] =
* [ [], [a], [b], [a, b] ]
* This keep working until add all elements.
*
* We filter to remove the combinations that are bigger than the max size
*
* @param size Max size of each result list
* @param restricted Should be exactly the received size.
* @return List of combinations
*/
public List<List<String>> getCombination(int size, boolean restricted) {
List<List<String>> result = pairs.parallelStream().reduce(
new ArrayList<>(),
(acc,current) -> {
if(acc.size() == 0) {
return current;
}
List<List<String>> combinations = new ArrayList<>();
current.stream().forEach(
(List<String> valueCurrent) -> acc.stream().forEach(
(List<String> valueAcc) -> {
List<String> combination = new ArrayList<>();
combination.addAll(valueAcc);
combination.addAll(valueCurrent);
if(combination.size() <= size) {
combinations.add(combination);
}
}
)
);
return combinations;
}
);
if(! restricted) {
return result;
}
/* if the combination is size restricted, filter only elements with the exactly size */
return result.stream().filter(combination -> combination.size() == size).
collect(Collectors.toList());
}
}
public class Main {
public static void main(String[] param) {
Combination combination = new Combination(Arrays.asList("A","B","C","D","E"));
/* show all the combinations from 0 to 3 elements */
System.out.println(combination.getCombination(3));
// [
// [],
// [A],
// [B], [A, B],
// [C], [A, C], [B, C], [A, B, C],
// [D], [A, D], [B, D], [A, B, D], [C, D], [A, C, D], [B, C, D],
// [E], [A, E], [B, E], [A, B, E], [C, E], [A, C, E], [B, C, E], [D, E], [A, D, E], [B, D, E], [C, D, E]
// ]
/* show all the combinations with exactly 4 elements */
System.out.println(combination.getCombination(4, true));
// [
// [A, B, C, D],
// [A, B, C, E],
// [A, B, D, E],
// [A, C, D, E],
// [B, C, D, E]
// ]
Combination other = Combination.createFromPairs(
new String[][]{{"1","2","3"},{"4","5","6"},{"7","8","9"},{"10","11","12"} }
);
/* show all the combinations with exactly 3 elements */
System.out.println(other.getCombination(3, true));
// [
// [1, 4, 7], [2, 4, 7], [3, 4, 7], [1, 5, 7] ...
// ... [3, 9, 12], [4, 9, 12], [5, 9, 12], [6, 9, 12]
// ]
}
}
Umm .... вы первый! –
Пожалуйста, напишите, что вы пробовали. – Phoenix
Извините, я не понял ..... что вы хотите сказать ??? –