fork download
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5.  
  6. class Combination {
  7. private List<List<List<String>>> pairs = new ArrayList<>();
  8.  
  9. public Combination( List<String> elements ) {
  10. this.pairs = createPairsFromElements( elements );
  11. }
  12.  
  13. private Combination() {
  14. }
  15.  
  16. public static Combination createFromPairs( String[][] arrPairs ) {
  17. Combination combination = new Combination();
  18. combination.pairs = createPairsFromArrayPairs(arrPairs);
  19. return combination;
  20. }
  21.  
  22. /**
  23.   * Create the pairs based on the String[][]
  24.   *
  25.   * Add the empty option at the beginner of each group
  26.   * Create the Lists and group them again
  27.   *
  28.   * @param arrPairs
  29.   * @return pairs
  30.   */
  31. private static List<List<List<String>>> createPairsFromArrayPairs( String[][] arrPairs ) {
  32. List<List<List<String>>> pairs = new ArrayList<>();
  33.  
  34. for ( String[] groupValue: arrPairs ) {
  35.  
  36. List<List<String>> listGroupValue = new ArrayList<>();
  37.  
  38. /* add the empty value to the pairs */
  39. listGroupValue.add(Arrays.asList());
  40.  
  41. for ( String value: groupValue ) {
  42.  
  43. List<String> listValue = Arrays.asList( value );
  44. listGroupValue.add( listValue );
  45.  
  46. }
  47.  
  48. pairs.add( listGroupValue );
  49. }
  50. return pairs;
  51.  
  52. }
  53.  
  54. /**
  55.   * Convert the string list "a", "b", "c" to
  56.   * this:
  57.   * [
  58.   * [ [], [a] ], [ [], [b] ], [ [], [c] ]
  59.   * ]
  60.   *
  61.   * @param elements List of values to the collection
  62.   * @return pairs
  63.   */
  64. private static List<List<List<String>>> createPairsFromElements( List<String> elements ) {
  65. return elements.stream().map(
  66. (String value) -> {
  67. List<List<String>> values = Arrays.asList(
  68. Arrays.asList(), // immutable empty list
  69. Arrays.asList( value ) // immutable atomic list
  70. );
  71. return values;
  72. }
  73. ).collect(Collectors.toList());
  74. }
  75.  
  76. /**
  77.   * Get the combination without restriction
  78.   *
  79.   * @param size
  80.   * @return
  81.   */
  82. public List<List<String>> getCombination( int size ) {
  83. return this.getCombination( size, false );
  84. }
  85.  
  86. /**
  87.   /*
  88.   * Combine every pair of elements creating a big list
  89.   * [ [], [b] ] x [ [], [a] ] = [ ([] + []), ([] + [a]), ([b] + []), ([b] + [a])) ] =
  90.   * [ [], [a], [b], [a, b] ]
  91.   * This keep working until add all elements.
  92.   *
  93.   * We filter to remove the combinations that are bigger than the max size
  94.   *
  95.   * @param size Max size of each result list
  96.   * @param restricted Should be exactly the received size.
  97.   * @return List of combinations
  98.   */
  99. public List<List<String>> getCombination( int size, boolean restricted ) {
  100.  
  101. List<List<String>> result = pairs.parallelStream().reduce(
  102. new ArrayList<>(),
  103. (acc,current) -> {
  104. if( acc.size() == 0 ) {
  105. return current;
  106. }
  107.  
  108. List<List<String>> combinations = new ArrayList<>();
  109.  
  110. current.stream().forEach(
  111. (List<String> valueCurrent ) -> acc.stream().forEach(
  112. (List<String> valueAcc) -> {
  113. List<String> combination = new ArrayList<>();
  114. combination.addAll( valueAcc );
  115. combination.addAll( valueCurrent );
  116. if( combination.size() <= size ) {
  117. combinations.add( combination );
  118. }
  119. }
  120. )
  121. );
  122. return combinations;
  123. }
  124. );
  125.  
  126. if( ! restricted ) {
  127. return result;
  128. }
  129.  
  130. /* if the combination is size restricted, filter only elements with the exactly size */
  131. return result.stream().filter( combination -> combination.size() == size ).
  132. collect(Collectors.toList());
  133. }
  134. }
  135.  
  136. public class Main {
  137.  
  138. public static void main( String[] param ) {
  139.  
  140. Combination combination = new Combination(Arrays.asList("A","B","C","D","E"));
  141.  
  142. /* show all the combinations from 0 to 3 elements */
  143. System.out.println( combination.getCombination(3));
  144. // [
  145. // [],
  146. // [A],
  147. // [B], [A, B],
  148. // [C], [A, C], [B, C], [A, B, C],
  149. // [D], [A, D], [B, D], [A, B, D], [C, D], [A, C, D], [B, C, D],
  150. // [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]
  151. // ]
  152.  
  153.  
  154. /* show all the combinations with exactly 4 elements */
  155. System.out.println( combination.getCombination(4, true));
  156. // [
  157. // [A, B, C, D],
  158. // [A, B, C, E],
  159. // [A, B, D, E],
  160. // [A, C, D, E],
  161. // [B, C, D, E]
  162. // ]
  163.  
  164.  
  165. Combination other = Combination.createFromPairs(
  166. new String[][]{{"1","2","3"},{"4","5","6"},{"7","8","9"},{"10","11","12"} }
  167. );
  168.  
  169.  
  170. /* show all the combinations with exactly 3 elements */
  171. System.out.println( other.getCombination(3, true));
  172. // [
  173. // [1, 4, 7], [2, 4, 7], [3, 4, 7], [1, 5, 7] ...
  174. // ... [3, 9, 12], [4, 9, 12], [5, 9, 12], [6, 9, 12]
  175. // ]
  176.  
  177. }
  178. }
  179.  
Success #stdin #stdout 0.21s 34984KB
stdin
Standard input is empty
stdout
[[], [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]]