### Question:
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);  }### Answer:
@Test public void empty() { List<List<Optional<Object>>> combinations = Combinator .combinations(Collections.emptyList()) .collect(Collectors.toList()); assertEquals(1, combinations.size()); assertEquals(0, combinations.get(0).size()); }
@Test public void twoNumbers() { List<List<Optional<Integer>>> combinations = Combinator .combinations(List.of(1, 1)) .collect(Collectors.toList()); assertEquals(4, combinations.size()); assertSameSize(2, combinations); assertContains(List.of(Optional.empty(), Optional.empty()), combinations); assertContains(List.of(Optional.empty(), Optional.of(1)), combinations); assertContains(List.of(Optional.of(1), Optional.empty()), combinations); assertContains(List.of(Optional.of(1), Optional.of(1)), combinations); }
@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); }