Skip to content

Instantly share code, notes, and snippets.

@gchiam
Created October 9, 2020 05:21
Show Gist options
  • Save gchiam/78227f4f7d2dd20a7e2f32bb3dc98b88 to your computer and use it in GitHub Desktop.
Save gchiam/78227f4f7d2dd20a7e2f32bb3dc98b88 to your computer and use it in GitHub Desktop.
Generate permutations of Boolean values
Stream.of(true, false)
.map(b2 -> Stream.of(true, false)
.map(b3 -> List.of(b2, b3))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
// [[true, true], [true, false], [false, true], [false, false]]
Stream.of(true, false)
.map(b1 -> Stream.of(true, false)
.map(b2 -> Stream.of(true, false)
.map(b3 -> List.of(b1, b2, b3)
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
// [[true, true, true], [true, true, false], [true, false, true], [true, false, false], [false, true, true], [false, true, false], [false, false, true], [false, false, false]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment