@Test public void threeStrings() { List<List<Optional<String>>> combinations = Combinator .combinations(List.of("a", "b", "c")) .collect(Collectors.toList()); assertEquals(8, combinations.size()); assertSameSize(3, combinations); assertContains(List.of(Optional.empty(), Optional.empty(), Optional.empty()), combinations); assertContains(List.of(Optional.empty(), Optional.empty(), Optional.of("c")), combinations); assertContains(List.of(Optional.empty(), Optional.of("b"), Optional.empty()), combinations); assertContains(List.of(Optional.empty(), Optional.of("b"), Optional.of("c")), combinations); assertContains(List.of(Optional.of("a"), Optional.empty(), Optional.empty()), combinations); assertContains(List.of(Optional.of("a"), Optional.empty(), Optional.of("c")), combinations); assertContains(List.of(Optional.of("a"), Optional.of("b"), Optional.empty()), combinations); assertContains(List.of(Optional.of("a"), Optional.of("b"), Optional.of("c")), combinations); }
public static <T> Stream<List<Optional<T>>> combinations(List<T> list) { long n = (long) Math.pow(2, list.size()); return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(n, Spliterator.SIZED) { long i = 0; @Override public boolean tryAdvance(Consumer<? super List<Optional<T>>> action) { if (i < n) { List<Optional<T>> out = new ArrayList<>(Long.bitCount(i)); for (int bit = 0; bit < list.size(); bit++) { if ((i & (1 << bit)) != 0) { out.add(Optional.of(list.get(bit))); } else { out.add(Optional.empty()); } } action.accept(out); ++i; return true; } else { return false; } } }, false); }
Combinator { public static <T> Stream<List<Optional<T>>> combinations(List<T> list) { long n = (long) Math.pow(2, list.size()); return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(n, Spliterator.SIZED) { long i = 0; @Override public boolean tryAdvance(Consumer<? super List<Optional<T>>> action) { if (i < n) { List<Optional<T>> out = new ArrayList<>(Long.bitCount(i)); for (int bit = 0; bit < list.size(); bit++) { if ((i & (1 << bit)) != 0) { out.add(Optional.of(list.get(bit))); } else { out.add(Optional.empty()); } } action.accept(out); ++i; return true; } else { return false; } } }, false); } }
Combinator { public static <T> Stream<List<Optional<T>>> combinations(List<T> list) { long n = (long) Math.pow(2, list.size()); return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(n, Spliterator.SIZED) { long i = 0; @Override public boolean tryAdvance(Consumer<? super List<Optional<T>>> action) { if (i < n) { List<Optional<T>> out = new ArrayList<>(Long.bitCount(i)); for (int bit = 0; bit < list.size(); bit++) { if ((i & (1 << bit)) != 0) { out.add(Optional.of(list.get(bit))); } else { out.add(Optional.empty()); } } action.accept(out); ++i; return true; } else { return false; } } }, false); } private  Combinator(); }
Combinator { public static <T> Stream<List<Optional<T>>> combinations(List<T> list) { long n = (long) Math.pow(2, list.size()); return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(n, Spliterator.SIZED) { long i = 0; @Override public boolean tryAdvance(Consumer<? super List<Optional<T>>> action) { if (i < n) { List<Optional<T>> out = new ArrayList<>(Long.bitCount(i)); for (int bit = 0; bit < list.size(); bit++) { if ((i & (1 << bit)) != 0) { out.add(Optional.of(list.get(bit))); } else { out.add(Optional.empty()); } } action.accept(out); ++i; return true; } else { return false; } } }, false); } private  Combinator(); static Stream<List<Optional<T>>> combinations(List<T> list); }
Combinator { public static <T> Stream<List<Optional<T>>> combinations(List<T> list) { long n = (long) Math.pow(2, list.size()); return StreamSupport.stream(new Spliterators.AbstractSpliterator<>(n, Spliterator.SIZED) { long i = 0; @Override public boolean tryAdvance(Consumer<? super List<Optional<T>>> action) { if (i < n) { List<Optional<T>> out = new ArrayList<>(Long.bitCount(i)); for (int bit = 0; bit < list.size(); bit++) { if ((i & (1 << bit)) != 0) { out.add(Optional.of(list.get(bit))); } else { out.add(Optional.empty()); } } action.accept(out); ++i; return true; } else { return false; } } }, false); } private  Combinator(); static Stream<List<Optional<T>>> combinations(List<T> list);  }