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() { } 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 */ List<List<List<String>>> pairs = new ArrayList<>(); List<List<String>> listGroupValue = new ArrayList<>(); /* add the empty value to the pairs */ 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( ); 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 { /* show all the combinations from 0 to 3 elements */ // [ // [], // [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 */ // [ // [A, B, C, D], // [A, B, C, E], // [A, B, D, E], // [A, C, D, E], // [B, C, D, E] // ] Combination other = Combination.createFromPairs( ); /* show all the combinations with exactly 3 elements */ // [ // [1, 4, 7], [2, 4, 7], [3, 4, 7], [1, 5, 7] ... // ... [3, 9, 12], [4, 9, 12], [5, 9, 12], [6, 9, 12] // ] } }
Standard input is empty
[[], [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]] [[A, B, C, D], [A, B, C, E], [A, B, D, E], [A, C, D, E], [B, C, D, E]] [[1, 4, 7], [2, 4, 7], [3, 4, 7], [1, 5, 7], [2, 5, 7], [3, 5, 7], [1, 6, 7], [2, 6, 7], [3, 6, 7], [1, 4, 8], [2, 4, 8], [3, 4, 8], [1, 5, 8], [2, 5, 8], [3, 5, 8], [1, 6, 8], [2, 6, 8], [3, 6, 8], [1, 4, 9], [2, 4, 9], [3, 4, 9], [1, 5, 9], [2, 5, 9], [3, 5, 9], [1, 6, 9], [2, 6, 9], [3, 6, 9], [1, 4, 10], [2, 4, 10], [3, 4, 10], [1, 5, 10], [2, 5, 10], [3, 5, 10], [1, 6, 10], [2, 6, 10], [3, 6, 10], [1, 7, 10], [2, 7, 10], [3, 7, 10], [4, 7, 10], [5, 7, 10], [6, 7, 10], [1, 8, 10], [2, 8, 10], [3, 8, 10], [4, 8, 10], [5, 8, 10], [6, 8, 10], [1, 9, 10], [2, 9, 10], [3, 9, 10], [4, 9, 10], [5, 9, 10], [6, 9, 10], [1, 4, 11], [2, 4, 11], [3, 4, 11], [1, 5, 11], [2, 5, 11], [3, 5, 11], [1, 6, 11], [2, 6, 11], [3, 6, 11], [1, 7, 11], [2, 7, 11], [3, 7, 11], [4, 7, 11], [5, 7, 11], [6, 7, 11], [1, 8, 11], [2, 8, 11], [3, 8, 11], [4, 8, 11], [5, 8, 11], [6, 8, 11], [1, 9, 11], [2, 9, 11], [3, 9, 11], [4, 9, 11], [5, 9, 11], [6, 9, 11], [1, 4, 12], [2, 4, 12], [3, 4, 12], [1, 5, 12], [2, 5, 12], [3, 5, 12], [1, 6, 12], [2, 6, 12], [3, 6, 12], [1, 7, 12], [2, 7, 12], [3, 7, 12], [4, 7, 12], [5, 7, 12], [6, 7, 12], [1, 8, 12], [2, 8, 12], [3, 8, 12], [4, 8, 12], [5, 8, 12], [6, 8, 12], [1, 9, 12], [2, 9, 12], [3, 9, 12], [4, 9, 12], [5, 9, 12], [6, 9, 12]]